One remarkable day company "X" received k machines. And they were not simple machines, they were mechanical programmers! This was the last unsuccessful step before switching to android programmers, but that's another story.The company has now n tasks, for each of them we know the start time of its execution si, the duration of its execution ti, and the company profit from its completion ci. Any machine can perform any task, exactly one at a time. If a machine has started to perform the task, it is busy at all moments of time from si to si + ti - 1, inclusive, and it cannot switch to another task.You are required to select a set of tasks which can be done with these k machines, and which will bring the maximum total profit.

The first line contains two integer numbers n and k (1 ≤ n ≤ 1000, 1 ≤ k ≤ 50) — the numbers of tasks and machines, correspondingly. The next n lines contain space-separated groups of three integers si, ti, ci (1 ≤ si, ti ≤ 109, 1 ≤ ci ≤ 106), si is the time where they start executing the i-th task, ti is the duration of the i-th task and ci is the profit of its execution.

Print n integers x1, x2, ..., xn. Number xi should equal 1, if task i should be completed and otherwise it should equal 0. If there are several optimal solutions, print any of them.

NoteIn the first sample the tasks need to be executed at moments of time 2 ... 8, 1 ... 3 and 4 ... 4, correspondingly. The first task overlaps with the second and the third ones, so we can execute either task one (profit 5) or tasks two and three (profit 6).

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0l, 0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%ld " (fun s -> s) in
      let t = Scanf.bscanf sb "%ld " (fun s -> s) in
      let c = Scanf.bscanf sb "%d " (fun s -> s) in
	a.(i) <- (s, t, c, i)
    done;
    Array.sort compare a;
  in
  let nv = 2 * n + 2 in
  let es = Array.make nv [] in
  let b = Array.make_matrix nv nv 0 in
  let c = Array.make_matrix nv nv 0 in
  let () =
    es.(0) <- [2];
    c.(0).(2) <- k;
    for i = 0 to n - 1 do
      let (s, t, cc, _) = a.(i) in
      let f = Int32.add s t in
      let j = ref (i + 1) in
	while !j < n && (let (s, _, _, _) = a.(!j) in s < f) do
	  incr j
	done;
	let w =
	  if !j = n
	  then 1
	  else (!j + 1) * 2
	in
	let u = 2 * i + 2 in
	let v = 2 * i + 3 in
	let x = if i < n - 1 then 2 * i + 4 else 1 in
	  es.(u) <- v :: es.(u);
	  es.(v) <- u :: es.(v);
	  c.(u).(v) <- 1;
	  b.(u).(v) <- -cc;
	  es.(u) <- x :: es.(u);
	  es.(x) <- u :: es.(x);
	  c.(u).(x) <- k;
	  es.(v) <- w :: es.(v);
	  es.(w) <- v :: es.(w);
	  c.(v).(w) <- 1;
(*Printf.printf "asd %d  %ld %ld  %d\n" i s t !j;*)
    done
  in
  let es = Array.map Array.of_list es in
  let f = Array.make_matrix nv nv 0 in
  let bellman_ford start finish =
    let p = Array.make nv (-1) in
    let w = Array.make nv 1000000000 in
    let stop = ref false in
      w.(start) <- 0;
      for k = 1 to nv do
	if not !stop then (
	  stop := true;
	  for u = 0 to nv - 1 do
	    for i = 0 to Array.length es.(u) - 1 do
	      let v = es.(u).(i) in
		if c.(u).(v) - f.(u).(v) > 0 then (
		  let w' =
		    if f.(u).(v) = 0
		    then b.(u).(v)
		    else -b.(v).(u)
		  in
		    if w.(u) < 1000000000 && w.(u) + w' < w.(v)
		    then (
		      w.(v) <- w.(u) + w';
		      p.(v) <- u;
		      stop := false;
		    )
		)
	    done
	  done
	)
      done;
      (p, w.(finish))
  in
  let augment start finish =
    let (p, w) = bellman_ford start finish in
(*Printf.printf "aug\n%!";*)
      if p.(finish) < 0
      then -1
      else (
	let v = ref finish in
	  while !v <> start do
	    let u = p.(!v) in
	      f.(u).(!v) <- f.(u).(!v) + 1;
	      f.(!v).(u) <- -f.(u).(!v);
(*Printf.printf "add %d %d\n" u !v;*)
	      v := u;
	  done;
	  w
      )
  in
  let r = Array.make n 0 in
    for i = 1 to k do
      augment 0 1
    done;
    for i = 0 to n - 1 do
      let (_, _, _, j) = a.(i) in
      let res =
	if f.(2 * i + 2).(2 * i + 3) > 0
	then 1
	else 0
      in
	r.(j) <- res
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d " r.(i)
    done;
    Printf.printf "\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0l, 0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%ld " (fun s -> s) in
      let t = Scanf.bscanf sb "%ld " (fun s -> s) in
      let c = Scanf.bscanf sb "%d " (fun s -> s) in
	a.(i) <- (s, t, c, i)
    done;
    Array.sort compare a;
  in
  let nv = 2 * n + 2 in
  let es = Array.make nv [] in
  let b = Array.make_matrix nv nv 0 in
  let c = Array.make_matrix nv nv 0 in
  let () =
    es.(0) <- [2];
    c.(0).(2) <- k;
    for i = 0 to n - 1 do
      let (s, t, cc, _) = a.(i) in
      let f = Int32.add s t in
      let j = ref (i + 1) in
	while !j < n && (let (s, _, _, _) = a.(!j) in s < f) do
	  incr j
	done;
	let w =
	  if !j = n
	  then 1
	  else (!j + 1) * 2
	in
	let u = 2 * i + 2 in
	let v = 2 * i + 3 in
	let x = if i < n - 1 then 2 * i + 4 else 1 in
	  es.(u) <- v :: es.(u);
	  es.(v) <- u :: es.(v);
	  c.(u).(v) <- 1;
	  b.(u).(v) <- -cc;
	  es.(u) <- x :: es.(u);
	  es.(x) <- u :: es.(x);
	  c.(u).(x) <- k;
	  es.(v) <- w :: es.(v);
	  es.(w) <- v :: es.(w);
	  c.(v).(w) <- k;
    done
  in
  let es = Array.map Array.of_list es in
  let f = Array.make_matrix nv nv 0 in
  let bellman_ford start finish =
    let p = Array.make nv (-1) in
    let w = Array.make nv 1000000000 in
      w.(start) <- 0;
      for k = 1 to nv do
	for u = 0 to nv - 1 do
	  for i = 0 to Array.length es.(u) - 1 do
	    let v = es.(u).(i) in
	      if c.(u).(v) - f.(u).(v) > 0 then (
		let w' =
		  if f.(u).(v) = 0
		  then b.(u).(v)
		  else -b.(v).(u)
		in
		  if w.(u) < 1000000000 && w.(u) + w' < w.(v)
		  then (
		    w.(v) <- w.(u) + w';
		    p.(v) <- u;
		  )
	      )
	  done
	done
      done;
      (p, w.(finish))
  in
  let augment start finish =
    let (p, w) = bellman_ford start finish in
      if p.(finish) < 0
      then -1
      else (
	let v = ref finish in
	  while !v <> start do
	    let u = p.(!v) in
	      f.(u).(!v) <- f.(u).(!v) + 1;
	      f.(!v).(u) <- -f.(u).(!v);
	      v := u;
	  done;
	  w
      )
  in
    for i = 1 to k do
      augment 0 1
    done;
    for i = 0 to n - 1 do
      let (_, _, _, i) = a.(i) in
      let res =
	if f.(2 * i + 2).(2 * i + 3) > 0
	then 1
	else 0
      in
	Printf.printf "%d " res
    done;
    Printf.printf "\n"

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let k = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n (0l, 0l, 0, 0) in
  let () =
    for i = 0 to n - 1 do
      let s = Scanf.bscanf sb "%ld " (fun s -> s) in
      let t = Scanf.bscanf sb "%ld " (fun s -> s) in
      let c = Scanf.bscanf sb "%d " (fun s -> s) in
	a.(i) <- (s, t, c, i)
    done;
    Array.sort compare a;
  in
  let nv = n + 2 in
  let es = Array.make nv [] in
  let b = Array.make_matrix nv nv 0 in
  let c = Array.make_matrix nv nv 0 in
  let () =
    es.(0) <- [2];
    c.(0).(2) <- k;
    for i = 0 to n - 1 do
      let (s, t, cc, _) = a.(i) in
      let f = Int32.add s t in
      let j = ref (i + 1) in
	while !j < n && (let (s, _, _, _) = a.(!j) in s < f) do
	  incr j
	done;
	let w =
	  if !j = n
	  then 1
	  else !j + 2
	in
	let u = i + 2 in
	let x = if i < n - 1 then i + 3 else 1 in
	  es.(u) <- w :: es.(u);
	  es.(w) <- u :: es.(w);
	  c.(u).(w) <- 1;
	  b.(u).(w) <- -cc;
	  es.(u) <- x :: es.(u);
	  es.(x) <- u :: es.(x);
	  c.(u).(x) <- k;
(*Printf.printf "asd %d  %ld %ld  %d\n" i s t !j;*)
    done
  in
  let es = Array.map Array.of_list es in
  let f = Array.make_matrix nv nv 0 in
  let bellman_ford start finish =
    let p = Array.make nv (-1) in
    let w = Array.make nv 1000000000 in
      w.(start) <- 0;
      for k = 1 to nv do
	for u = 0 to nv - 1 do
	  for i = 0 to Array.length es.(u) - 1 do
	    let v = es.(u).(i) in
	      if c.(u).(v) - f.(u).(v) > 0 then (
		let w' =
		  if f.(u).(v) = 0
		  then b.(u).(v)
		  else -b.(v).(u)
		in
		  if w.(u) < 1000000000 && w.(u) + w' < w.(v)
		  then (
		    w.(v) <- w.(u) + w';
		    p.(v) <- u;
		  )
	      )
	  done
	done
      done;
      (p, w.(finish))
  in
  let augment start finish =
    let (p, w) = bellman_ford start finish in
(*Printf.printf "aug\n%!";*)
      if p.(finish) < 0
      then -1
      else (
	let v = ref finish in
	  while !v <> start do
	    let u = p.(!v) in
	      f.(u).(!v) <- f.(u).(!v) + 1;
	      f.(!v).(u) <- -f.(u).(!v);
(*Printf.printf "add %d %d\n" u !v;*)
	      v := u;
	  done;
	  w
      )
  in
  let r = Array.make n 0 in
    for i = 1 to k do
      augment 0 1
    done;
    for i = 0 to n - 1 do
      let (_, _, _, j) = a.(i) in
      let x = if i < n - 1 then i + 3 else 1 in
      let res =
	if f.(i + 2).(x) > 0
	then 1
	else 0
      in
	r.(j) <- res
    done;
    for i = 0 to n - 1 do
      Printf.printf "%d " r.(i)
    done;
    Printf.printf "\n"


Recently, on the course of algorithms and data structures, Valeriy learned how to use a deque. He built a deque filled with $$$n$$$ elements. The $$$i$$$-th element is $$$a_i$$$ ($$$i$$$ = $$$1, 2, \ldots, n$$$). He gradually takes the first two leftmost elements from the deque (let's call them $$$A$$$ and $$$B$$$, respectively), and then does the following: if $$$A > B$$$, he writes $$$A$$$ to the beginning and writes $$$B$$$ to the end of the deque, otherwise, he writes to the beginning $$$B$$$, and $$$A$$$ writes to the end of the deque. We call this sequence of actions an operation.For example, if deque was $$$[2, 3, 4, 5, 1]$$$, on the operation he will write $$$B=3$$$ to the beginning and $$$A=2$$$ to the end, so he will get $$$[3, 4, 5, 1, 2]$$$.The teacher of the course, seeing Valeriy, who was passionate about his work, approached him and gave him $$$q$$$ queries. Each query consists of the singular number $$$m_j$$$ $$$(j = 1, 2, \ldots, q)$$$. It is required for each query to answer which two elements he will pull out on the $$$m_j$$$-th operation.Note that the queries are independent and for each query the numbers $$$A$$$ and $$$B$$$ should be printed in the order in which they will be pulled out of the deque.Deque is a data structure representing a list of elements where insertion of new elements or deletion of existing elements can be made from both sides.

The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 \leq n \leq 10^5$$$, $$$0 \leq q \leq 3 \cdot 10^5$$$) — the number of elements in the deque and the number of queries. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$, where $$$a_i$$$ $$$(0 \leq a_i \leq 10^9)$$$ — the deque element in $$$i$$$-th position. The next $$$q$$$ lines contain one number each, meaning $$$m_j$$$ ($$$1 \leq m_j \leq 10^{18}$$$).

For each teacher's query, output two numbers $$$A$$$ and $$$B$$$ — the numbers that Valeriy pulls out of the deque for the $$$m_j$$$-th operation.

let readInt () = Scanf.scanf " %d" (fun x -> x)
let readInt64 () = Scanf.scanf " %Ld" (fun x -> x)

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

type solver = {n: int; a: int array; k: int; cyca: int array}

let precomp (n, a) =
  (* No fold_*i :( *)
  let maxi = ref 0 in
  for i=1 to n-1 do
    if a.(i) > a.(!maxi) then maxi := i
  done;
  let maxi = !maxi in
  let cyca = Array.make (maxi + n) (-1) in
  Array.blit a 0 cyca 0 n;
  for i=0 to maxi-1 do
    let x = cyca.(i) in
    let y = cyca.(i+1) in
    let x = max x y and y = min x y in
    cyca.(i+1) <- x;
    cyca.(i+n) <- y
  done;
  {n; a; k=maxi; cyca}

let query {n; a; k; cyca} m =
  let m =
    let open Int64 in
    let n = of_int n in
    let k = of_int k in
    to_int (if m < k then m else (m -- k) %% (n -- 1L) ++ k)
  in
  if m < k then (cyca.(m), a.(m+1))
  else (cyca.(k), cyca.(m+1))

let main () =
  let n = readInt () in
  let q = readInt () in
  let a = Array.init n (fun _ -> readInt ()) in
  let s = precomp (n, a) in
  for i=0 to q-1 do
    let m = readInt64 () -- 1L in
    let (x, y) = query s m in
    Printf.printf "%d %d\n" x y
  done

let () = main ()

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

let () =
  let n, q = Scanf.scanf "%d %d\n" (fun x y -> x, y) in
  let a = Array.init n (fun _ -> Scanf.scanf "%Ld " (fun x -> x)) in
  let max = ref a.(0)in
  let max_idx = ref 0 in
  for i = 1 to n-1 do
    if a.(i) > !max then begin max := a.(i); max_idx := i end;
  done;
  let b = Array.make (n-1) 0L in
  for i = 0 to n- !max_idx - 2 do
    b.(i) <- a.(!max_idx + 1 + i);
  done;
  let first = ref a.(0) in
  let c = Array.make !max_idx (a.(0), a.(1)) in
  for i = 1 to !max_idx do
    c.(i-1) <- (!first, a.(i));
    if a.(i) <= !first then
      b.(n- !max_idx - 2 + i) <- a.(i)
    else begin
      b.(n- !max_idx - 2 + i) <- !first;
      first := a.(i);
    end;
  done;

  for i = 1 to q do
    let m = Scanf.scanf "%Ld " (fun x -> x) in
    if m > Int64.of_int !max_idx then begin
      let r = Int64.rem (m -- Int64.of_int !max_idx -- 1L) (Int64.of_int (n-1)) |> Int64.to_int in
      Printf.printf "%Ld %Ld\n" !max b.(r)
    end
    else
      let m = Int64.to_int m in
      let x, y = c.(m-1) in
      Printf.printf "%Ld %Ld\n" x y;
  done
;;


Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table. Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.

The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50). Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.

Print a single number — the minimum number of operations Simon needs to carry out his idea.

NoteIn the first sample, the sequence of operations can be like this:    For the first time you need to choose cell (2, 2) and corner (1, 1).  For the second time you need to choose cell (2, 2) and corner (3, 3).  For the third time you need to choose cell (2, 2) and corner (3, 1).  For the fourth time you need to choose cell (2, 2) and corner (1, 3). In the second sample the sequence of operations can be like this:    For the first time you need to choose cell (3, 1) and corner (4, 3).  For the second time you need to choose cell (2, 3) and corner (1, 1).

let (|>) x f = f x ;;
let (@@) f x = f x ;;

let ident x = x ;;
let ident2 x y = (x, y) ;;
let ident3 x y z = (x, y, z) ;;
let ident4 x y z w = (x, y, z, w) ;;
let ident5 x y z w v = (x, y, z, w, v) ;;

let char_to_int x = int_of_char x - int_of_char '0' ;;
let int_to_char x = char_of_int (x + int_of_char '0') ;;

module List = struct
  include ListLabels ;;

  let rec iteri i f = function
    | [] -> ()
    | a::l -> f i a; iteri (i + 1) f l

  let iteri ~f l = iteri 0 f l
end
;;

module Array = struct
  include ArrayLabels ;;

  let reduce ~f xs =
    let acc = ref xs.(0) in
    for i = 1 to length xs - 1 do
      acc := f !acc xs.(i)
    done;
    !acc
  ;;
end
;;

module String = struct
  include StringLabels ;;

  let map_to_list ~f ss =
    let res = ref [] in
    iter ss ~f:(fun c -> res := f c :: !res);
    List.rev !res
  ;;

  let of_char_list ss =
    let res = String.create @@ List.length ss in
    List.iteri ss ~f:(fun i c -> res.[i] <- c);
    res
  ;;

  let iteri ~f a =
    for i = 0 to length a - 1 do f i (unsafe_get a i) done
  ;;
end
;;

module Float = struct
  let (+) = (+.)
  let (-) = (-.)
  let ( * ) = ( *. )
  let (/) = (/.)
end
;;

module Int64 = struct
  include Int64
  let (+) = add
  let (-) = sub
  let ( * ) = mul
  let (/) = div
end
;;

let fold_for ?(skip=1) min max ~init ~f =
  if skip = 0 then failwith "fold_for";
  let cmp = if skip > 0 then (<) else (>) in

  let rec iter cur acc =
    if not @@ cmp cur max then acc
    else iter (cur + skip) (f acc cur) in

  iter min init
;;

let iter_for ?(skip=1) min max ~f =
  fold_for ~skip:skip min max ~init:() ~f:(fun _ i -> f i)
;;

let input_n n fmt ~f =
  fold_for 0 n ~init:[] ~f:(fun acc _ ->
    Scanf.scanf fmt f :: acc) |> List.rev
;;

let () =
  let n, m = Scanf.scanf "%d %d " ident2 in
  try
    iter_for 0 n ~f:(fun i ->
      iter_for 0 m ~f:(fun j ->
        let x = Scanf.scanf "%d " ident in
        if (i = 0 || i = n - 1 || j = 0 || j = m - 1) && x = 1 then raise Exit));
    print_endline "4";
  with
  | _ -> print_endline "2";
;;


Greg has an array a = a1, a2, ..., an and m operations. Each operation looks as: li, ri, di, (1 ≤ li ≤ ri ≤ n). To apply operation i to the array means to increase all array elements with numbers li, li + 1, ..., ri by value di.Greg wrote down k queries on a piece of paper. Each query has the following form: xi, yi, (1 ≤ xi ≤ yi ≤ m). That means that one should apply operations with numbers xi, xi + 1, ..., yi to the array.Now Greg is wondering, what the array a will be after all the queries are executed. Help Greg.

The first line contains integers n, m, k (1 ≤ n, m, k ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105) — the initial array. Next m lines contain operations, the operation number i is written as three integers: li, ri, di, (1 ≤ li ≤ ri ≤ n), (0 ≤ di ≤ 105). Next k lines contain the queries, the query number i is written as two integers: xi, yi, (1 ≤ xi ≤ yi ≤ m). The numbers in the lines are separated by single spaces.

On a single line print n integers a1, a2, ..., an — the array after executing all the queries. Separate the printed numbers by spaces. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.

open Printf;;
open Scanf;;
let getll()=bscanf Scanning.stdib " %Ld " (fun x->x);;
let getint()=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 n=getint();;
let m=getint();;
let k=getint();;
let aa=Array.init (n+1) (function 0->0L|_->getll());;
let o1=Array.init (m+1) (function 0->(0,0,0)|_ -> let a=getint()in let b=getint()in let c=getint() in (a,b,c));;
let o2=Array.init (k+1) (function 0->(0,0)|_ -> let a=getint()in let b=getint()in (a,b));;
let a=Array.create (m+2) 0;;
let b=Array.create (n+2) 0L;;
for i=1 to k do
	let l=fst o2.(i)
	and r=snd o2.(i) in
		a.(l)<-a.(l)+1;
		a.(r+1)<-a.(r+1)-1;
done;;
let sumup a=let len=Array.length a in for i=1 to len-1 do a.(i)<-a.(i-1)+a.(i); done;;
let sumupl a=let len=Array.length a in for i=1 to len-1 do a.(i)<-a.(i-1)++a.(i); done;;
sumup a;;
let ff c=function (l,r,x)->let d=Int64.of_int(x)**Int64.of_int(c)in b.(l)<-b.(l)++d;b.(r+1)<-b.(r+1)--d;;
for i=1 to m do ff a.(i) o1.(i); done;;
sumupl b;;
for i=1 to n do
	if i>1 then print_char ' ';
	printf "%Ld" (aa.(i)++b.(i));
done;;
print_newline();;

open Printf
open Scanf

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 read_long () = bscanf Scanning.stdib " %Ld " (fun x -> x)
let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let () =
  let n = read_int() in
  let m = read_int() in
  let k = read_int() in
  let a = Array.init (n+1) (fun i -> if i=0 then 0L else read_long()) in
  let op = Array.init (m+1) (fun i -> if i=0 then (0,0,0L) else
			       let l = read_int() in
			       let r = read_int() in
			       let d = read_long() in
				 (l,r,d)
			    ) in
  let q = Array.init (k+1) (fun i -> if i=0 then (0,0) else
			      let l = read_int() in
			      let r = read_int() in
				(l,r)
			   ) in

  let opcount = Array.make (m+2) 0L in
  let rcount = Array.make (n+2) 0L in

    for i=1 to k do
      let (l,r) = q.(i) in
      opcount.(l) <- opcount.(l) ++ 1L;
      opcount.(r+1) <- opcount.(r+1) -- 1L;
    done;

    for i=1 to m do
      opcount.(i) <- opcount.(i-1) ++ opcount.(i)
    done;

    for i=1 to m do
      let (l,r,d) = op.(i) in
	rcount.(l) <- rcount.(l) ++ (d ** opcount.(i));
	rcount.(r+1) <- rcount.(r+1) -- (d ** opcount.(i));
    done;

    for i=1 to n do
      rcount.(i) <- rcount.(i-1) ++ rcount.(i);
    done;

    for i=1 to n do
      if i>1 then printf " ";
      printf "%Ld" (rcount.(i) ++ a.(i))
    done;

    print_newline()


Alice got an array of length $$$n$$$ as a birthday present once again! This is the third year in a row! And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice.Bob has chosen $$$m$$$ changes of the following form. For some integer numbers $$$x$$$ and $$$d$$$, he chooses an arbitrary position $$$i$$$ ($$$1 \le i \le n$$$) and for every $$$j \in [1, n]$$$ adds $$$x + d \cdot dist(i, j)$$$ to the value of the $$$j$$$-th cell. $$$dist(i, j)$$$ is the distance between positions $$$i$$$ and $$$j$$$ (i.e. $$$dist(i, j) = |i - j|$$$, where $$$|x|$$$ is an absolute value of $$$x$$$).For example, if Alice currently has an array $$$[2, 1, 2, 2]$$$ and Bob chooses position $$$3$$$ for $$$x = -1$$$ and $$$d = 2$$$ then the array will become $$$[2 - 1 + 2 \cdot 2,~1 - 1 + 2 \cdot 1,~2 - 1 + 2 \cdot 0,~2 - 1 + 2 \cdot 1]$$$ = $$$[5, 2, 1, 3]$$$. Note that Bob can't choose position $$$i$$$ outside of the array (that is, smaller than $$$1$$$ or greater than $$$n$$$).Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric.What is the maximum arithmetic mean value Bob can achieve?

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$) — the number of elements of the array and the number of changes. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$d_i$$$ ($$$-10^3 \le x_i, d_i \le 10^3$$$) — the parameters for the $$$i$$$-th change.

Print the maximal average arithmetic mean of the elements Bob can achieve. Your answer is considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$.

open Printf open Scanf
module MyInt64 = struct
	(* include Int64 *)
	(* int *)
	let (+$) = (+)
	let (-$) = (-)
	let (/$) = (/)
	let ( *$) = ( * )
	(* int64 *)
	let (+) p q = Int64.add p q
	let (-) p q = Int64.sub p q
	let ( * ) p q = Int64.mul p q
	let (/) p q = Int64.div p q

	let labs p = if p < 0L then -1L*p else p
	(* let (~|) p = Int64.of_int p *)
	let (~|) p = Int64.to_int p
	let (~~|) p = Int64.of_int p

	let (+=) r v = r := !r + v
	let (-=) r v = r := !r - v
	let ( *=) r v= r := !r * v
	let (/=) r v = r := !r / v

	let input_i64_array n =
		Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
	let get_i64 () = Scanf.scanf " %Ld" (fun v -> v)
	let get_2_i64 () = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
	let get_3_i64 () = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
	let get_4_i64 () = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
	let get_5_i64 () = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
	let print_i64_endline n =
		n |> Int64.to_string |> print_endline
end open MyInt64
let () =
	let n,m = get_2_i64 ()
	in let a = Array.make ~|m (0L,0L)
	in for i=0 to ~|m-$1 do
		a.(i) <- get_2_i64 ()
	done;

	let mid = (n+1L)/2L in
	let sum_left = ref 0L in
	let sum_mid = ref 0L in
	for i=0 to ~|n-$1 do
		sum_left := !sum_left + labs (mid - (~~|i+1L));
		sum_mid := !sum_mid + labs (~~|i);
	done;
	Array.fold_left (fun u v ->
		let x,d = v in
		u + x * n
			+ d * if d<0L then !sum_left else !sum_mid
	) 0L a
	|> (fun v -> printf "%.10f\n" (Int64.to_float v /. Int64.to_float n))

open Printf open Scanf
let input_int_array n =
	Array.init n (fun _ -> Scanf.scanf " %d" (fun v -> v))
let get_int () = Scanf.scanf " %d" (fun v -> v)
let get_2_ints () = Scanf.scanf " %d %d" (fun u v -> u,v)
let get_3_ints () = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w)
let get_4_ints () = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x)
let get_5_ints () = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)

let print_int_endline n = n |> string_of_int |> print_endline

let () =
	let n,m = get_2_ints ()
	in let a = Array.make m (0,0)
	in for i=0 to m-1 do
		a.(i) <- get_2_ints ()
	done;

	let mid = (n+1)/2 in
	let sum_left = ref 0 in
	let sum_mid = ref 0 in
	for i=0 to n-1 do
		sum_left := !sum_left + abs (mid - (i+1));
		sum_mid := !sum_mid + abs (i);
	done;
	Array.fold_left (fun u v ->
		let x,d = v in
		u + x * n
			+ d * if d<0 then !sum_left else !sum_mid
	) 0 a
	|> (fun v -> printf "%.10f\n" (float_of_int v /. float_of_int n))


Vova promised himself that he would never play computer games... But recently Firestorm — a well-known game developing company — published their newest game, World of Farcraft, and it became really popular. Of course, Vova started playing it.Now he tries to solve a quest. The task is to come to a settlement named Overcity and spread a rumor in it.Vova knows that there are n characters in Overcity. Some characters are friends to each other, and they share information they got. Also Vova knows that he can bribe each character so he or she starts spreading the rumor; i-th character wants ci gold in exchange for spreading the rumor. When a character hears the rumor, he tells it to all his friends, and they start spreading the rumor to their friends (for free), and so on.The quest is finished when all n characters know the rumor. What is the minimum amount of gold Vova needs to spend in order to finish the quest?Take a look at the notes if you think you haven't understood the problem completely.

The first line contains two integer numbers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) — the number of characters in Overcity and the number of pairs of friends. The second line contains n integer numbers ci (0 ≤ ci ≤ 109) — the amount of gold i-th character asks to start spreading the rumor. Then m lines follow, each containing a pair of numbers (xi, yi) which represent that characters xi and yi are friends (1 ≤ xi, yi ≤ n, xi ≠ yi). It is guaranteed that each pair is listed at most once.

Print one number — the minimum amount of gold Vova has to spend in order to finish the quest.

NoteIn the first example the best decision is to bribe the first character (he will spread the rumor to fourth character, and the fourth one will spread it to fifth). Also Vova has to bribe the second and the third characters, so they know the rumor.In the second example Vova has to bribe everyone.In the third example the optimal decision is to bribe the first, the third, the fifth, the seventh and the ninth characters.

open Int64;;
let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let n = scan_int() and m= scan_int() in let odw = Array.make n 0 and wyn = ref zero
and p = Array.init n (fun x ->x) and w = Array.init n (fun x->scan_int())  in
let rec find x =
  if p.(x) = x then x
  else begin
	p.(x) <- find p.(x);
    p.(x)
  end
in let uni x y =
  let u = find x and v = find y in
  if u <> v then
    if w.(u) < w.(v) then p.(v) <- u else p.(u) <- v
in begin for i=1 to m do
  let u = scan_int () and v = scan_int() in uni (u-1) (v-1)
done;
for i=0 to (n-1) do
  if odw.(find i) = 0 then begin
    odw.(find i) <- 1; wyn := add (!wyn) (Int64.of_int(w.(find i))) end
done;
print_string(Int64.to_string(!wyn))
end

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let scan_string () = Scanf.scanf " %s" (fun x -> x);;
let psp () = print_string " ";;
let b2i x = if x == true then 1 else 0;;

module type GRAPH_LABELS =
  sig
    type label
  end;;

(* Typ modułu implementującego grafy o wierzchołkach numerowanych od 0 do n-1. *)
module type GRAPH =
  sig
    (* Etykiety krawędzi. *)
    type label

    (* Typ grafów o wierzchołkach typu 'a. *)
    type graph

    (* Pusty graf. *)
    val init : int -> graph

    (* Rozmiar grafu *)
    val size : graph -> int

    (* Dodanie krawędzi skierowanej łączącej dwa wierzchołki. *)
    val insert_directed_edge : graph -> int -> int -> label -> unit

    (* Dodanie krawędzi nieskierowanej (dwukierunkowej) łączącej dwa wierzchołki. *)
    val insert_edge : graph -> int -> int -> label -> unit

    (* Lista incydencji danego wierzchołka. *)
    val neighbours :  graph -> int -> (int*label) list
  end;;

module Graph = functor (L : GRAPH_LABELS) ->
(struct
  (* Etykiety krawędzi. *)
  type label = L.label

  (* Typ grafów - tablica list sąsiedztwa. *)
  type graph = {n : int; e : (int*label) list array}

  (* Pusty graf. *)
  let init s = {n = s; e = Array.make s []}

  (* Rozmiar grafu. *)
  let size g = g.n

  (* Dodanie krawędzi skierowanej łączącej dwa wierzchołki. *)
  let insert_directed_edge g x y l =
    assert ((x<>y)  && (x >= 0) && (x < size g) && (y >= 0) && (y < size g));
    g.e.(x) <- (y,l)::g.e.(x)

  (* Dodanie krawędzi łączącej dwa (istniejące) wierzchołki. *)
  let insert_edge g x y l =
    insert_directed_edge g x y l;
    insert_directed_edge g y x l

  (* Lista incydencji danego wierzchołka. *)
  let neighbours g x =
    g.e.(x)
end : GRAPH with type label = L.label);;

module IntLabel =
  struct
    type label = int
  end;;

module G = Graph(IntLabel);;

open G;;

let n = scan_int();;
let m = scan_int();;

let g = G.init(n);;
let values = Array.make n 0;;

for i = 0 to n - 1 do
  let x = scan_int() in
  values.(i) <- x;
done;

for i = 0 to m - 1 do
  let (x, y) = (scan_int() - 1, scan_int() - 1) in
  insert_edge g x y 1;
done;;

let vis = Array.make n false;;

let rec dfs x =
  vis.(x) <- true;
  let res = ref values.(x) in
  List.iter (fun(y, _) ->
    if vis.(y) = false then res := min (!res) (dfs y)
  ) (neighbours g x);
  !res;;

open Int64;;
let sumek = ref zero;;

for i = 0 to n - 1 do
  if vis.(i) = false then sumek := add (!sumek) (Int64.of_int(dfs i));
done;

print_string(Int64.to_string(!sumek));;

open Printf
open Scanf

let ( ++ ) a b = Int64.add a b

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

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

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

  let cost = Array.init n (fun i -> long(read_int())) in
  let comp = Array.make n (-1) in

  let graph = Array.make n [] in

  for i=0 to m-1 do
    let (u,v) = read_pair() in
    let (u,v) = (u-1,v-1) in
    graph.(u) <- v::graph.(u);
    graph.(v) <- u::graph.(v)
  done;

  let rec dfs u p c =
    if comp.(u) = -1 then (
      comp.(u) <- c;
      List.iter (fun v -> if v<>p then dfs v u c) graph.(u)
    )
  in

  let rec loop i c = if i=n then c else (
    if comp.(i) = -1 then (
      dfs i (-1) c;
      loop (i+1) (c+1)
    ) else loop (i+1) c
  ) in

  let ncomps = loop 0 0 in

  let mincomp = Array.make ncomps 1_000_000_001L in

  for i=0 to n-1 do
    mincomp.(comp.(i)) <- min cost.(i) mincomp.(comp.(i))
  done;

  let answer = sum 0 (ncomps-1) (fun i -> mincomp.(i)) in

  printf "%Ld\n" answer

let scan_int () = Scanf.scanf " %d" (fun x -> x);;
let n = scan_int() and m= scan_int() in let odw = Array.make n 0 and wyn = ref 0
and p = Array.init n (fun x ->x) and w = Array.init n (fun x->scan_int())  in
let rec find x =
  if p.(x) = x then x
  else begin
	p.(x) <- find p.(x);
    p.(x)
  end
in let uni x y =
  let u = find x and v = find y in
  if u <> v then
    if w.(u) < w.(v) then p.(v) <- u else p.(u) <- v
in begin for i=1 to m do
  let u = scan_int () and v = scan_int() in uni (u-1) (v-1)
done;
for i=0 to (n-1) do
  if odw.(find i) = 0 then begin
    odw.(find i) <- 1; wyn := !wyn + w.(find i) end
done;
print_int (!wyn)
end


Unlike Knights of a Round Table, Knights of a Polygonal Table deprived of nobility and happy to kill each other. But each knight has some power and a knight can kill another knight if and only if his power is greater than the power of victim. However, even such a knight will torment his conscience, so he can kill no more than $$$k$$$ other knights. Also, each knight has some number of coins. After a kill, a knight can pick up all victim's coins.Now each knight ponders: how many coins he can have if only he kills other knights?You should answer this question for each knight.

The first line contains two integers $$$n$$$ and $$$k$$$ $$$(1 \le n \le 10^5, 0 \le k \le \min(n-1,10))$$$ — the number of knights and the number $$$k$$$ from the statement. The second line contains $$$n$$$ integers $$$p_1, p_2 ,\ldots,p_n$$$ $$$(1 \le p_i \le 10^9)$$$ — powers of the knights. All $$$p_i$$$ are distinct. The third line contains $$$n$$$ integers $$$c_1, c_2 ,\ldots,c_n$$$ $$$(0 \le c_i \le 10^9)$$$ — the number of coins each knight has.

Print $$$n$$$ integers — the maximum number of coins each knight can have it only he kills other knights.

NoteConsider the first example.   The first knight is the weakest, so he can't kill anyone. That leaves him with the only coin he initially has.  The second knight can kill the first knight and add his coin to his own two.  The third knight is the strongest, but he can't kill more than $$$k = 2$$$ other knights. It is optimal to kill the second and the fourth knights: $$$2+11+33 = 46$$$.  The fourth knight should kill the first and the second knights: $$$33+1+2 = 36$$$. In the second example the first knight can't kill anyone, while all the others should kill the one with the index less by one than their own.In the third example there is only one knight, so he can't kill anyone.

let ( */ ) = Big_int.mult_big_int;;
let ( +/ ) = Big_int.add_big_int;;
let ( -/ ) = Big_int.sub_big_int;;
let to_int = Big_int.int_of_big_int;;
let of_int = Big_int.big_int_of_int;;
let min = Big_int.min_big_int;;
let (modi) = Big_int.mod_big_int;;
let to_string = Big_int.string_of_big_int;;
let of_string = Big_int.big_int_of_string;;
let (</) = Big_int.lt_big_int;;

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

exception End;;

module SetI = Set.Make(struct
    type t = Big_int.big_int * int
    let compare (x,a) (y,b) = match to_int @@ x -/ y
      with 0 -> compare a b | c -> c
  end);;

let n,k = Scanf.sscanf (read_line ()) "%i %i" (fun n k -> n,k)  in
let power = read_line () |> split_on_char ' ' |> List.map of_string  |> Array.of_list in
let coins = read_line () |> split_on_char ' ' |> List.map of_string |> Array.of_list in
let result = Array.make n (of_int 0) in
let sorted_power = Array.mapi (fun i x -> (x,i)) power in
Array.sort (fun (x,_) (y,_) -> to_int @@ x -/ y) sorted_power;
let _ = Array.fold_left (fun s (p, i) ->
    result.(i) <- SetI.fold (fun (c,_) t -> c +/t) s coins.(i);
    if SetI.cardinal s < k  then SetI.add (coins.(i),i) s
    else if (not (SetI.is_empty s)) && fst @@ SetI.min_elt s </ coins.(i) then SetI.add (coins.(i), i) (SetI.remove (SetI.min_elt s) s) else s
) SetI.empty sorted_power in
for i = 0 to n - 1 do
  Printf.printf "%s " (to_string result.(i));
done;
print_newline ();

let ( */ ) = Big_int.mult_big_int;;
let ( +/ ) = Big_int.add_big_int;;
let ( -/ ) = Big_int.sub_big_int;;
let to_int = Big_int.int_of_big_int;;
let of_int = Big_int.big_int_of_int;;
let min = Big_int.min_big_int;;
let (modi) = Big_int.mod_big_int;;
let to_string = Big_int.string_of_big_int;;
let of_string = Big_int.big_int_of_string;;
let (</) = Big_int.lt_big_int;;

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

exception End;;

module SetI = Set.Make(struct
    type t = Big_int.big_int
    let compare x y = to_int @@ x -/ y
  end);;

let n,k = Scanf.sscanf (read_line ()) "%i %i" (fun n k -> n,k)  in
let power = read_line () |> split_on_char ' ' |> List.map of_string  |> Array.of_list in
let coins = read_line () |> split_on_char ' ' |> List.map of_string |> Array.of_list in
let result = Array.make n (of_int 0) in
let sorted_power = Array.mapi (fun i x -> (x,i)) power in
Array.sort (fun (x,_) (y,_) -> to_int @@ x -/ y) sorted_power;
let _ = Array.fold_left (fun s (p, i) ->
    result.(i) <- SetI.fold (fun c t -> c +/t) s coins.(i);
    if SetI.cardinal s < k || SetI.cardinal s = 0 then SetI.add coins.(i) s
    else if SetI.min_elt s </ coins.(i) then SetI.add coins.(i) (SetI.remove (SetI.min_elt s) s) else s
) SetI.empty sorted_power in
for i = 0 to n - 1 do
  Printf.printf "%s " (to_string result.(i));
done;
print_newline ();

let ( */ ) = Big_int.mult_big_int;;
let ( +/ ) = Big_int.add_big_int;;
let ( -/ ) = Big_int.sub_big_int;;
let to_int = Big_int.int_of_big_int;;
let of_int = Big_int.big_int_of_int;;
let min = Big_int.min_big_int;;
let (modi) = Big_int.mod_big_int;;
let to_string = Big_int.string_of_big_int;;
let of_string = Big_int.big_int_of_string;;
let (</) = Big_int.lt_big_int;;

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

exception End;;

module SetI = Set.Make(struct
    type t = Big_int.big_int
    let compare x y = to_int @@ x -/ y
  end);;

let n,k = Scanf.sscanf (read_line ()) "%i %i" (fun n k -> n,k)  in
let power = read_line () |> split_on_char ' ' |> List.map of_string  |> Array.of_list in
let coins = read_line () |> split_on_char ' ' |> List.map of_string |> Array.of_list in
let result = Array.make n (of_int 0) in
let sorted_power = Array.mapi (fun i x -> (x,i)) power in
Array.sort (fun (x,_) (y,_) -> to_int @@ x -/ y) sorted_power;
let _ = Array.fold_left (fun s (p, i) ->
    result.(i) <- SetI.fold (fun c t -> c +/t) s coins.(i);
    if SetI.cardinal s < k  then SetI.add coins.(i) s
    else if (not (SetI.is_empty s)) && SetI.min_elt s </ coins.(i) then SetI.add coins.(i) (SetI.remove (SetI.min_elt s) s) else s
) SetI.empty sorted_power in
for i = 0 to n - 1 do
  Printf.printf "%s " (to_string result.(i));
done;
print_newline ();

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

let mapi2 f a b =
  if Array.length a <> Array.length b then invalid_arg "map2";
  Array.init (Array.length a) (fun i -> f i (Array.unsafe_get a i) (Array.unsafe_get b i))
;;

let iteri2 f a b =
  for o = 0 to Array.length a-1 do
    f o (Array.unsafe_get a o) (Array.unsafe_get b o)
  done
;;

let custom_insert a x =
  if a.(0) < x then (a.(0) <- x; Array.sort compare a)
;;

exception End;;

let n,k = Scanf.sscanf (read_line ()) "%i %i" (fun n k -> n,k)  in
let power = read_line () |> split_on_char ' ' |> List.map int_of_string  |> Array.of_list in
let coins = read_line () |> split_on_char ' ' |> List.map int_of_string |> Array.of_list in
let sorted_coins = Array.mapi (fun i x -> (x,i)) coins in
Array.sort (fun (x,_) (y,_) -> compare y x) sorted_coins;
for i = 0 to n-1 do
  let kill = ref 0 in
  let count = ref coins.(i) in
  begin try
      Array.iter (fun (c,j) -> if power.(j) < power.(i) then (count := !count + c; incr kill);
                   if !kill = k then raise End ) sorted_coins;
    with End -> ();
  end;
  Printf.printf "%i " !count;
done;
print_newline ();

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

let mapi2 f a b =
  if Array.length a <> Array.length b then invalid_arg "map2";
  Array.init (Array.length a) (fun i -> f i (Array.unsafe_get a i) (Array.unsafe_get b i))
;;

let iteri2 f a b =
  for o = 0 to Array.length a-1 do
    f o (Array.unsafe_get a o) (Array.unsafe_get b o)
  done
;;

let custom_insert a x =
  if a.(0) < x then (a.(0) <- x; Array.sort compare a)
;;

exception End;;

let n,k = Scanf.sscanf (read_line ()) "%i %i" (fun n k -> n,k)  in
let power = read_line () |> split_on_char ' ' |> List.map int_of_string  |> Array.of_list in
let coins = read_line () |> split_on_char ' ' |> List.map int_of_string |> Array.of_list in
let sorted_coins = Array.mapi (fun i x -> (x,i)) coins in
Array.sort (fun (x,_) (y,_) -> compare y x) sorted_coins;
for i = 0 to n-1 do
  let kill = ref 0 in
  let count = ref coins.(i) in
  begin try
      Array.iter (fun (c,j) -> if power.(j) < power.(i) && c >= 0 then (count := !count + c; incr kill);
                   if !kill = k then raise End ) sorted_coins;
    with End -> ();
  end;
  Printf.printf "%i " !count;
done;
print_newline ();

let ( */ ) = Big_int.mult_big_int;;
let ( +/ ) = Big_int.add_big_int;;
let ( -/ ) = Big_int.sub_big_int;;
let to_int = Big_int.int_of_big_int;;
let of_int = Big_int.big_int_of_int;;
let min = Big_int.min_big_int;;
let (modi) = Big_int.mod_big_int;;
let to_string = Big_int.string_of_big_int;;

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

exception End;;

let n,k = Scanf.sscanf (read_line ()) "%i %i" (fun n k -> n,k)  in
let power = read_line () |> split_on_char ' ' |> List.map int_of_string  |> Array.of_list in
let coins = read_line () |> split_on_char ' ' |> List.map int_of_string |> Array.of_list in
let sorted_coins = Array.mapi (fun i x -> (of_int x,i)) coins in
Array.sort (fun (x,_) (y,_) -> to_int @@ y -/ x) sorted_coins;
for i = 0 to n-1 do
  let kill = ref 0 in
  let count = ref @@ of_int coins.(i) in
  begin try
      Array.iter (fun (c,j) -> if power.(j) < power.(i) then (count := !count +/ c; incr kill);
                   if !kill = k then raise End ) sorted_coins;
    with End -> ();
  end;
  Printf.printf "%s " (to_string !count);
done;
print_newline ();


We have a secret array. You don't know this array and you have to restore it. However, you know some facts about this array:  The array consists of $$$n$$$ distinct positive (greater than $$$0$$$) integers.  The array contains two elements $$$x$$$ and $$$y$$$ (these elements are known for you) such that $$$x < y$$$.  If you sort the array in increasing order (such that $$$a_1 < a_2 < \ldots < a_n$$$), differences between all adjacent (consecutive) elements are equal (i.e. $$$a_2 - a_1 = a_3 - a_2 = \ldots = a_n - a_{n-1})$$$. It can be proven that such an array always exists under the constraints given below.Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize $$$\max(a_1, a_2, \dots, a_n)$$$.You have to answer $$$t$$$ independent test cases.

The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 \le n \le 50$$$; $$$1 \le x < y \le 50$$$) — the length of the array and two elements that are present in the array, respectively.

For each test case, print the answer: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn't matter). It can be proven that such an array always exists under the given constraints.

let line_of_numbers () =
  ()
  |> read_line
  |> Str.split (Str.regexp " ")
  |> List.map (fun s -> s |> String.trim |> int_of_string)

let () =
  let [t] = line_of_numbers () in
  for _ = 1 to t do
    try
      let [n; x; y] = line_of_numbers () in
      let d = y - x in
      for s = 1 to d do
        if d mod s == 0 then
          let sc = d / s in
          let rs = n - (sc + 1) in
          if rs >= 0 then
            let st = x - (min rs ((x - 1) / s)) * s in
            for i = 0 to (n - 1) do
              st + i * s
              |> string_of_int
              |> print_string;
              print_char ' ';
            done;
            print_endline "";
            raise_notrace Exit
      done
    with
      Exit -> ()
  done

let line_of_numbers () =
  ()
  |> read_line
  |> Str.split (Str.regexp " ")
  |> List.map (fun s -> s |> String.trim |> int_of_string)

let () =
  let [t] = line_of_numbers () in
  for _ = 1 to t do
    try
      let [n; x; y] = line_of_numbers () in
      let d = y - x in
      for s = 1 to d do
        if d mod s == 0 then
          let sc = d / s in
          let rs = n - (sc + 1) in
          if rs >= 0 then
            let st = x - (min rs (x / s)) * s in
            for i = 0 to (n - 1) do
              st + i * s
              |> string_of_int
              |> print_string;
              print_char ' ';
            done;
            print_endline "";
            raise_notrace Exit
      done
    with
      Exit -> ()
  done


Recently, Masha was presented with a chessboard with a height of $$$n$$$ and a width of $$$m$$$.The rows on the chessboard are numbered from $$$1$$$ to $$$n$$$ from bottom to top. The columns are numbered from $$$1$$$ to $$$m$$$ from left to right. Therefore, each cell can be specified with the coordinates $$$(x,y)$$$, where $$$x$$$ is the column number, and $$$y$$$ is the row number (do not mix up).Let us call a rectangle with coordinates $$$(a,b,c,d)$$$ a rectangle lower left point of which has coordinates $$$(a,b)$$$, and the upper right one — $$$(c,d)$$$.The chessboard is painted black and white as follows:   An example of a chessboard. Masha was very happy with the gift and, therefore, invited her friends Maxim and Denis to show off. The guys decided to make her a treat — they bought her a can of white and a can of black paint, so that if the old board deteriorates, it can be repainted. When they came to Masha, something unpleasant happened: first, Maxim went over the threshold and spilled white paint on the rectangle $$$(x_1,y_1,x_2,y_2)$$$. Then after him Denis spilled black paint on the rectangle $$$(x_3,y_3,x_4,y_4)$$$.To spill paint of color $$$color$$$ onto a certain rectangle means that all the cells that belong to the given rectangle become $$$color$$$. The cell dyeing is superimposed on each other (if at first some cell is spilled with white paint and then with black one, then its color will be black).Masha was shocked! She drove away from the guests and decided to find out how spoiled the gift was. For this, she needs to know the number of cells of white and black color. Help her find these numbers!

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^3$$$) — the number of test cases. Each of them is described in the following format: The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 10^9$$$) — the size of the board. The second line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 \le x_1 \le x_2 \le m, 1 \le y_1 \le y_2 \le n$$$) — the coordinates of the rectangle, the white paint was spilled on. The third line contains four integers $$$x_3$$$, $$$y_3$$$, $$$x_4$$$, $$$y_4$$$ ($$$1 \le x_3 \le x_4 \le m, 1 \le y_3 \le y_4 \le n$$$) — the coordinates of the rectangle, the black paint was spilled on.

Output $$$t$$$ lines, each of which contains two numbers — the number of white and black cells after spilling paint, respectively.

NoteExplanation for examples:The first picture of each illustration shows how the field looked before the dyes were spilled. The second picture of each illustration shows how the field looked after Maxim spoiled white dye (the rectangle on which the dye was spilled is highlighted with red). The third picture in each illustration shows how the field looked after Denis spoiled black dye (the rectangle on which the dye was spilled is highlighted with red).In the first test, the paint on the field changed as follows:  In the second test, the paint on the field changed as follows:  In the third test, the paint on the field changed as follows:  In the fourth test, the paint on the field changed as follows:  In the fifth test, the paint on the field changed as follows:

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array
let hoge _ = scanf " %Ld %Ld %Ld %Ld" @@ fun p q r s -> p-1L,q-1L,r-1L,s-1L
let () =
  let t = get_i64 0 in
  rep 1L t (fun _ ->
    let n,m = get_2_i64 0 in
    let whites p q r s =
      let lx,ly = r-p+1L,s-q+1L in
      let white_lt = match p mod 2L,q mod 2L with
      | 0L,0L | 1L,1L -> 1L
      | _ -> 0L
      in
      match lx mod 2L,ly mod 2L with
      | 0L,0L -> lx*ly / 2L
      | 1L,1L -> (lx-1L)*(ly-1L)/2L + (lx-1L)/2L + (ly-1L)/2L + white_lt
      | 1L,0L -> lx*ly / 2L
      | 0L,1L -> lx*ly / 2L
      | _ -> fail 0 in
    let blacks p q r s = (r-p+1L)*(s-q+1L) - whites p q r s in
    let x,y,z,w = hoge 0 in
    let p,q,r,s = hoge 0
    in
    let a,b,c,d = max x p,max y q,min z r,min w s in
    let isec = if a>c || b>d then 0L else blacks a b c d in
    let white =
      whites 0L 0L (n-1L) (m-1L)
      + blacks x y z w
      - whites p q r s
      - isec in
    let black =
      blacks 0L 0L (n-1L) (m-1L)
      - blacks x y z w
      + whites p q r s
      + isec
    in assert (white + black = n*m);
    printf "%Ld %Ld\n" white black;
    (* printf "  %Ld %Ld\n"
      (whites 0L 0L (n-1L) (m-1L))
      (blacks 0L 0L (n-1L) (m-1L));
    printf "  %Ld %Ld\n"
      (whites 0L 0L (n-1L) (m-1L) + blacks x y z w)
      (blacks 0L 0L (n-1L) (m-1L) - blacks x y z w); *)
  )


Mirror Box is a name of a popular game in the Iranian National Amusement Park (INAP). There is a wooden box, 105 cm long and 100 cm high in this game. Some parts of the box's ceiling and floor are covered by mirrors. There are two negligibly small holes in the opposite sides of the box at heights hl and hr centimeters above the floor. The picture below shows what the box looks like.  In the game, you will be given a laser gun to shoot once. The laser beam must enter from one hole and exit from the other one. Each mirror has a preset number vi, which shows the number of points players gain if their laser beam hits that mirror. Also — to make things even funnier — the beam must not hit any mirror more than once.Given the information about the box, your task is to find the maximum score a player may gain. Please note that the reflection obeys the law "the angle of incidence equals the angle of reflection".

The first line of the input contains three space-separated integers hl, hr, n (0 < hl, hr < 100, 0 ≤ n ≤ 100) — the heights of the holes and the number of the mirrors. Next n lines contain the descriptions of the mirrors. The i-th line contains space-separated vi, ci, ai, bi; the integer vi (1 ≤ vi ≤ 1000) is the score for the i-th mirror; the character ci denotes i-th mirror's position — the mirror is on the ceiling if ci equals "T" and on the floor if ci equals "F"; integers ai and bi (0 ≤ ai < bi ≤ 105) represent the x-coordinates of the beginning and the end of the mirror. No two mirrors will share a common point. Consider that the x coordinate increases in the direction from left to right, so the border with the hole at height hl has the x coordinate equal to 0 and the border with the hole at height hr has the x coordinate equal to 105.

The only line of output should contain a single integer — the maximum possible score a player could gain.

NoteThe second sample is depicted above. The red beam gets 10 + 50 + 5 + 35 + 8 + 2 = 110 points and the blue one gets 120.The red beam on the picture given in the statement shows how the laser beam can go approximately, this is just illustration how the laser beam can gain score. So for the second sample there is no such beam that gain score 110.

let _ =
  let w = 100000 in
  let h = 100 in
  let sb = Scanf.Scanning.stdib in
  let hl = Scanf.bscanf sb "%d " (fun s -> s) in
  let hr = Scanf.bscanf sb "%d " (fun s -> s) in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let v = Array.make n 0 in
  let b = Array.make n false in
  let u = Array.make w (-1) in
  let d = Array.make w (-1) in
  let res = ref 0 in
  let () =
    for i = 0 to n - 1 do
      let vv = Scanf.bscanf sb "%d " (fun s -> s) in
      let c = Scanf.bscanf sb "%s " (fun s -> s) in
      let a = Scanf.bscanf sb "%d " (fun s -> s) in
      let b = Scanf.bscanf sb "%d " (fun s -> s) in
      let aa = if c = "T" then u else d in
	v.(i) <- vv;
	for j = a to b - 1 do
	  aa.(j) <- i;
	done
    done;
  in
  let solve hl hr u d =
    for i = 1 to 100 do
      Array.fill b 0 n false;
      let y =
	(i - 1) * h +
	  if i mod 2 = 0
	  then h - hr
	  else hr
      in
      let r = ref 0 in
	try
	  for j = 0 to i - 1 do
	    let z = j * h in
	    let x =
	      float_of_int (z + hl) *. float_of_int w /. float_of_int (y + hl)
	    in
	      (*Printf.printf "asd %f  %d %d %d\n" x hl y z;*)
	    let x = int_of_float x in
	    let a = if j mod 2 = 0 then d else u in
	      if a.(x) < 0 || b.(a.(x))
	      then raise Not_found
	      else (
		(*Printf.printf "hit %d %d %d\n" i a.(x) v.(a.(x));*)
		b.(a.(x)) <- true;
		r := !r + v.(a.(x));
	      )
	  done;
	  res := max !res !r;
	with
	  | Not_found -> ()
    done;
  in
    solve hl hr u d;
    solve (h - hl) (h - hr) d u;
    Printf.printf "%d\n" !res


Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with n meteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bn respectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.Well, she almost had it all — each of the sequences a and b matched exactly n - 1 elements in Sengoku's permutation. In other words, there is exactly one i (1 ≤ i ≤ n) such that ai ≠ pi, and exactly one j (1 ≤ j ≤ n) such that bj ≠ pj.For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.

The first line of input contains a positive integer n (2 ≤ n ≤ 1 000) — the length of Sengoku's permutation, being the length of both meteor outbursts at the same time. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of colours in the first meteor outburst. The third line contains n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of colours in the second meteor outburst. At least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.

Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them. Input guarantees that such permutation exists.

NoteIn the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) + a) 0
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)
let minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)

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

  let hist = Array.make (n+1) 0 in

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

  let (_,dup) = maxf 1 n (fun i -> (hist.(i), i)) in
  let (_,missing) = minf 1 n (fun i -> (hist.(i), i)) in
  (* dup is the thing that occurs twice in a *)
  (* missing is the thing that does not occur in a *)

  let rec loop i ac = if i = n then ac else
      if a.(i) = dup then loop (i+1) (i::ac) else loop (i+1) ac
  in

  let dupindex = loop 0 [] in

  let i1 = List.nth dupindex 0 in
  let i2 = List.nth dupindex 1 in

  let p1 = Array.init n (fun i -> if i=i1 then missing else a.(i)) in
  let p2 = Array.init n (fun i -> if i=i2 then missing else a.(i)) in

  let diff1 = sum 0 (n-1) (fun i -> if b.(i) <> p1.(i) then 1 else 0) in
  let diff2 = sum 0 (n-1) (fun i -> if b.(i) <> p2.(i) then 1 else 0) in

  let answer = if diff1 = 1 then p1 else if diff2 = 1 then p2 else failwith "bad" in

  for i=0 to n-1 do
    printf "%d " answer.(i)
  done;
  print_newline()


We get more and more news about DDoS-attacks of popular websites.Arseny is an admin and he thinks that a website is under a DDoS-attack if the total number of requests for a some period of time exceeds $$$100 \cdot t$$$, where $$$t$$$ — the number of seconds in this time segment. Arseny knows statistics on the number of requests per second since the server is booted. He knows the sequence $$$r_1, r_2, \dots, r_n$$$, where $$$r_i$$$ — the number of requests in the $$$i$$$-th second after boot. Determine the length of the longest continuous period of time, which Arseny considers to be a DDoS-attack. A seeking time period should not go beyond the boundaries of the segment $$$[1, n]$$$.

The first line contains $$$n$$$ ($$$1 \le n \le 5000$$$) — number of seconds since server has been booted. The second line contains sequence of integers $$$r_1, r_2, \dots, r_n$$$ ($$$0 \le r_i \le 5000$$$), $$$r_i$$$ — number of requests in the $$$i$$$-th second.

Print the only integer number — the length of the longest time period which is considered to be a DDoS-attack by Arseny. If it doesn't exist print 0.

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

let () =
	let n = read_int () in
	let t = Array.make (n + 1) 0 in
	for i = 1 to n do
		let a = read_int () in
		t.(i) <- a;
	done;
	let res = ref 0 in
	for i = 1 to n do
		let sum = ref 0 in
		for j = i to n do
			sum := !sum + t.(j);
			if !sum > (j - i + 1) * 100 then
				res := max (j - i + 1) (!res);
		done;

	done;
	printf "%d\n" !res


Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out n numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect.The lesson was long, and Vasya has written all the correct ways to place exactly k pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109 + 7. Help him!

The first line contains two integers, n and k (0 ≤ k < n ≤ 105). The second line contains a string consisting of n digits.

Print the answer to the problem modulo 109 + 7.

NoteIn the first sample the result equals (1 + 08) + (10 + 8) = 27.In the second sample the result equals 1 + 0 + 8 = 9.

open Printf
open Scanf

let p = 1_000_000_007L

let long x = Int64.of_int x
let short x = Int64.to_int x
let ( %% ) a b = Int64.rem a b
let ( ** ) a b = (Int64.mul a b) %% p
let ( ++ ) a b = (Int64.add a b) %% p
let ( -- ) a b = ((Int64.sub a b) ++ p)

let inverse a =
  let rec egcd a b =
    (* given a and b this returns (x, y, g) where g = gcd(a,b) and ax+by=g  *)
    if a = 0 then (0,1,b) else
      let (x,y,g) = egcd (b mod a) a in
      (y-(b/a)*x, x, g)
  in
  let (x,_,_) = egcd (short a) (short p) in
  long (if x >= 0 then x else x+(short p))

let init_factorial n =
  let f = Array.make (n+1) 1L in
  for i=1 to n do
    f.(i) <- (long i) ** f.(i-1)
  done;
  f

let binomial n k f =  (* n choose k (f is the factoral table) *)
  if k < 0 || n < k then 0L else
    f.(n) ** (inverse f.(k)) ** (inverse f.(n-k))

let d2n c = (int_of_char c) - (int_of_char '0')

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)
let () =
  let n = read_int () in
  let k = read_int () in
  let dig = read_string () in

  let fact = init_factorial n in

  let rec scan i s p ac = if i=n then ac else ( (* p=10^i  *)
    let c = s ++ p ** (binomial (n-1-i) k fact) in
    let ac = ac ++ (long (d2n dig.[n-i-1])) ** c in
    let f = p ** (binomial (n-2-i) (k-1) fact) in
    let s = s ++ f in
    let p = 10L ** p in
    scan (i+1) s p ac
  ) in

  printf "%Ld\n" (scan 0 0L 1L 0L)


You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$. Friends asked you to make the greatest common divisor (GCD) of all numbers in the array equal to $$$1$$$. In one operation, you can do the following: Select an arbitrary index in the array $$$1 \leq i \leq n$$$; Make $$$a_i = \gcd(a_i, i)$$$, where $$$\gcd(x, y)$$$ denotes the GCD of integers $$$x$$$ and $$$y$$$. The cost of such an operation is $$$n - i + 1$$$.You need to find the minimum total cost of operations we need to perform so that the GCD of the all array numbers becomes equal to $$$1$$$.

Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 5\,000$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 20$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array.

For each test case, output a single integer — the minimum total cost of operations that will need to be performed so that the GCD of all numbers in the array becomes equal to $$$1$$$. We can show that it's always possible to do so.

NoteIn the first test case, the GCD of the entire array is already equal to $$$1$$$, so there is no need to perform operations.In the second test case, select $$$i = 1$$$. After this operation, $$$a_1 = \gcd(2, 1) = 1$$$. The cost of this operation is $$$1$$$.In the third test case, you can select $$$i = 1$$$, after that the array $$$a$$$ will be equal to $$$[1, 4]$$$. The GCD of this array is $$$1$$$, and the total cost is $$$2$$$.In the fourth test case, you can select $$$i = 2$$$, after that the array $$$a$$$ will be equal to $$$[3, 2, 9]$$$. The GCD of this array is $$$1$$$, and the total cost is $$$2$$$.In the sixth test case, you can select $$$i = 4$$$ and $$$i = 5$$$, after that the array $$$a$$$ will be equal to $$$[120, 60, 80, 4, 5]$$$. The GCD of this array is $$$1$$$, and the total cost is $$$3$$$.

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

let read_ints () =
    let line = read_line () in
    let ints = Str.split (Str.regexp "  *") line in
    List.map int_of_string ints;;

let t = (List.hd (read_ints()))
in	for i = 1 to t do
		let n = (List.hd (read_ints()))
		in	let arr = read_ints()
			in	let g = List.fold_left gcd 0 arr in
				let x = n-1 in
				if g = 1 then print_string "0\n" else
					if (gcd g n) = 1 then print_string "1\n" else
						if (gcd g x) = 1 then print_string "2\n" else
							print_string "3\n"
done


Luntik came out for a morning stroll and found an array $$$a$$$ of length $$$n$$$. He calculated the sum $$$s$$$ of the elements of the array ($$$s= \sum_{i=1}^{n} a_i$$$). Luntik calls a subsequence of the array $$$a$$$ nearly full if the sum of the numbers in that subsequence is equal to $$$s-1$$$.Luntik really wants to know the number of nearly full subsequences of the array $$$a$$$. But he needs to come home so he asks you to solve that problem!A sequence $$$x$$$ is a subsequence of a sequence $$$y$$$ if $$$x$$$ can be obtained from $$$y$$$ by deletion of several (possibly, zero or all) elements.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The next $$$2 \cdot t$$$ lines contain descriptions of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 60$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^9$$$) — the elements of the array $$$a$$$.

For each test case print the number of nearly full subsequences of the array.

NoteIn the first test case, $$$s=1+2+3+4+5=15$$$, only $$$(2,3,4,5)$$$ is a nearly full subsequence among all subsequences, the sum in it is equal to $$$2+3+4+5=14=15-1$$$.In the second test case, there are no nearly full subsequences.In the third test case, $$$s=1+0=1$$$, the nearly full subsequences are $$$(0)$$$ and $$$()$$$ (the sum of an empty subsequence is $$$0$$$).

let rec count n n0 n1 =
  match n with
  | 0 -> (n0, n1)
  | _ -> let x = Scanf.scanf " %d" (fun x -> x)
    in match x with
    | 0 -> count (n - 1) (n0 + 1) n1
    | 1 -> count (n - 1) n0 (n1 + 1)
    | _ -> count (n - 1) n0 n1

let solve a b: Int64.t =
  Int64.mul (Int64.shift_left (Int64.of_int 1) a) (Int64.of_int b)

let () =
  let tc = Scanf.scanf " %d" (fun x -> x) in
  let rec loop tc =
    match tc with
    | 0 -> ()
    | _ -> let n = Scanf.scanf " %d" (fun x -> x) in
      let a, b = count n 0 0 in
      Printf.printf "%Ld\n" (solve a b); loop (tc -1)
  in loop tc

let rec count n n0 n1 =
  match n with
  | 0 -> (n0, n1)
  | _ -> let x = Scanf.scanf " %d" (fun x -> x)
    in match x with
    | 0 -> count (n - 1) (n0 + 1) n1
    | 1 -> count (n - 1) n0 (n1 + 1)
    | _ -> count (n - 1) n0 n1

let solve a b: Int64.t =
  Int64.mul (Int64.shift_left (Int64.of_int a) 1) (Int64.of_int b)

let () =
  let tc = Scanf.scanf " %d" (fun x -> x) in
  let rec loop tc =
    match tc with
    | 0 -> ()
    | _ -> let n = Scanf.scanf " %d" (fun x -> x) in
      let a, b = count n 0 0 in
      Printf.printf "%Ld\n" (solve a b); loop (tc -1)
  in loop tc

let rec count n n0 n1 =
  match n with
  | 0 -> (n0, n1)
  | _ -> let x = Scanf.scanf " %d" (fun x -> x)
    in match x with
    | 0 -> count (n - 1) (n0 + 1) n1
    | 1 -> count (n - 1) n0 (n1 + 1)
    | _ -> count (n - 1) n0 n1

let solve a b =
  (1 lsl a) * b

let () =
  let tc = Scanf.scanf " %d" (fun x -> x) in
  let rec loop tc =
    match tc with
    | 0 -> ()
    | _ -> let n = Scanf.scanf " %d" (fun x -> x) in
      let a, b = count n 0 0 in
      Printf.printf "%d\n" (solve a b); loop (tc -1)
  in loop tc


There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a way on it that   starts in the upper left cell of the matrix;  each following cell is to the right or down from the current cell;  the way ends in the bottom right cell. Moreover, if we multiply together all the numbers along the way, the result should be the least "round". In other words, it should end in the least possible number of zeros.

The first line contains an integer number n (2 ≤ n ≤ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).

In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.

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

let cnt n k : int =
  let rec go n acc =
    if n = 0 then
      1000000
    else
      if n mod k <> 0 then
        acc
      else
        go (n/k) (acc+1)
  in go n 0

let dp n a k : int*string =
  let b : int array array = Array.init n (fun i ->
    Array.init n (fun j -> cnt a.(i).(j) k)) in
  for i = 1 to n-1 do
    b.(i).(0) <- b.(i).(0)+b.(i-1).(0);
    b.(0).(i) <- b.(0).(i)+b.(0).(i-1)
  done;
  for i = 1 to n-1 do
    for j = 1 to n-1 do
      b.(i).(j) <- b.(i).(j) + min b.(i-1).(j) b.(i).(j-1)
    done
  done;
  let plan = String.make (2*n-2) ' ' in
  let rec go i j =
    if i > 0 || j > 0 then
      if i = 0 || (j > 0 && b.(i).(j-1) < b.(i-1).(j)) then (
        plan.[i+j-1] <- 'R';
        go i (j-1)
      ) else (
        plan.[i+j-1] <- 'D';
        go (i-1) j
      ) in
  go (n-1) (n-1);
  b.(n-1).(n-1), plan

let () =
  let n = read_int 0 in
  let a : int array array = Array.init n (fun _ -> Array.init n read_int) in
  let two, two_plan = dp n a 2
  and five, five_plan = dp n a 5 in
  let zx, zy = ref (-1), ref (-1) in
  for i = 0 to n-1 do
    for j = 0 to n-1 do
      if a.(i).(j) = 0 then (
        zx := i;
        zy := j
      )
    done
  done;
  let ans = min two five in
  if ans > 0 && !zx <> (-1) then (
    print_endline "1";
    for i = 1 to !zx do
      print_char 'D'
    done;
    for i = 1 to !zy do
      print_char 'R'
    done;
    for i = !zx+1 to n-1 do
      print_char 'D'
    done;
    for i = !zy+1 to n-1 do
      print_char 'R'
    done
  ) else
    Printf.printf "%d\n%s\n" ans (if two < five then two_plan else five_plan)


There is a fence in front of Polycarpus's home. The fence consists of n planks of the same width which go one after another from left to right. The height of the i-th plank is hi meters, distinct planks can have distinct heights.    Fence for n = 7 and h = [1, 2, 6, 1, 1, 7, 1] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly k consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such k consecutive planks that the sum of their heights is minimal possible.Write the program that finds the indexes of k consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic).

The first line of the input contains integers n and k (1 ≤ n ≤ 1.5·105, 1 ≤ k ≤ n) — the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers h1, h2, ..., hn (1 ≤ hi ≤ 100), where hi is the height of the i-th plank of the fence.

Print such integer j that the sum of the heights of planks j, j + 1, ..., j + k - 1 is the minimum possible. If there are multiple such j's, print any of them.

NoteIn the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8.

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

let () =
	let n = read_int ()
	and m = read_int ()
	and wyn = ref 100000000
	and indeks = ref 1 in

	let tab = Array.init n read_int
	and pref = Array.make (n + 1) 0 in

	for i = 1 to n do
		pref.(i) <- pref.(i - 1) + tab.(i - 1);
	done;

	for i = 1 to n - m + 1 do
		let roznica = pref.(i + m - 1) - pref.(i - 1) in
		if roznica < !wyn then begin
			wyn := roznica;
			indeks := i;
		end;
	done;

	printf "%d\n" !indeks;;

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

let list_init n =
    let rec loop i list =
        if i < n
        then
            let element = read () in
            loop (i + 1) (element :: list)
        else
            List.rev list
    in loop 0 []

let input () =
    let n = read () in
    let k = read () in
    let list = list_init n in
    (n, k, list)

let get_sums list =
    let rec loop list sum sum_list =
        match list with
            | (head :: tail) ->
                let sum = head + sum in
                loop tail sum (sum :: sum_list)
            | [] -> List.rev sum_list
    in loop list 0 [0]

let solve (n, k, list) =
    let fence = get_sums list in
    let arr = Array.of_list fence in
    let rec loop i j min =
        if i + k <= n
        then
            let diff = arr.(i + k) - arr.(i) in
            if diff < min
            then
                loop (i + 1) i diff
            else
                loop (i + 1) j min
        else
            j + 1
    in loop 0 (n + 1) 1000000000

let print result = Printf.printf "%d\n" result

let () = print (solve (input ()))

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

let () =
	let n = read_int ()
	and m = read_int ()
	and wyn = ref 100000000
	and indeks = ref 1 in

	let tab = Array.init n read_int
	and pref = Array.make (n + 1) 0 in

	for i = 1 to n do
		pref.(i) <- pref.(i - 1) + tab.(i - 1);
	done;

	for i = 1 to n - m + 1 do
		let roznica = pref.(i + m - 1) - pref.(i) in
		if roznica < !wyn then begin
			wyn := roznica;
			indeks := i;
		end;
	done;

	printf "%d\n" !indeks;;

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

let list_init n =
    let rec loop i list =
        if i < n
        then
            let element = read () in
            loop (i + 1) (element :: list)
        else
            List.rev list
    in loop 0 []

let input () =
    let n = read () in
    let k = read () in
    let list = list_init n in
    (n, k, list)

let get_sums list =
    let rec loop list sum sum_list =
        match list with
            | (head :: tail) ->
                let sum = head + sum in
                loop tail sum (sum :: sum_list)
            | [] -> List.rev sum_list
    in loop list 0 [0]

let solve (n, k, list) =
    let fence = get_sums list in
    let arr = Array.of_list fence in
    let rec loop i j min =
        if i + k < n
        then
            let diff = arr.(i + k) - arr.(i) in
            if diff < min
            then
                loop (i + 1) i diff
            else
                loop (i + 1) j min
        else
            j + 1
    in loop 0 (n + 1) 1500001

let print result = Printf.printf "%d\n" result

let () = print (solve (input ()))

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

let list_init n =
    let rec loop i list =
        if i < n
        then
            let element = read () in
            loop (i + 1) (element :: list)
        else
            List.rev list
    in loop 0 []

let input () =
    let n = read () in
    let k = read () in
    let list = list_init n in
    (n, k, list)

let get_sums list =
    let rec loop list sum sum_list =
        match list with
            | (head :: tail) ->
                let sum = head + sum in
                loop tail sum (sum :: sum_list)
            | [] -> List.rev sum_list
    in loop list 0 [0]

let solve (n, k, list) =
    let fence = get_sums list in
    let arr = Array.of_list fence in
    let rec loop i j min =
        if i + k <= n
        then
            let diff = arr.(i + k) - arr.(i) in
            if diff < min
            then
                loop (i + 1) i diff
            else
                loop (i + 1) j min
        else
            j + 1
    in loop 0 (n + 1) 1500001

let print result = Printf.printf "%d\n" result

let () = print (solve (input ()))


Given an array a1, a2, ..., an of n integers, find the largest number in the array that is not a perfect square.A number x is said to be a perfect square if there exists an integer y such that x = y2.

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array. The second line contains n integers a1, a2, ..., an ( - 106 ≤ ai ≤ 106) — the elements of the array. It is guaranteed that at least one element of the array is not a perfect square.

Print the largest number in the array which is not a perfect square. It is guaranteed that an answer always exists.

NoteIn the first sample case, 4 is a perfect square, so the largest number in the array that is not a perfect square is 2.

let get_int () = Scanf.scanf " %d" (fun i -> i)
let get_float () = Scanf.scanf " %f" (fun i -> i)

let is_sqrt x =
  let r = int_of_float @@ sqrt (float_of_int x) in
  r * r = x

let get_array n =
  let rec aux acc k =
    if k = 0 then acc
    else aux (get_int () :: acc) (k - 1)
  in aux [] n

let main () =
  let n = get_int () in
  let rec aux m k =
    if k = 0 then m
    else
      let x = get_int () in
      if not (is_sqrt x) && x > m then aux x (k - 1)
      else aux m (k - 1)
  in
  let ans = aux (-1_000_000) n in
  Printf.printf "%d" ans

let () = main ()

(* Mish test sort - worked *)

(* you can use either gr or rdln but not both *)
let gr () = Scanf.scanf " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let rdln() = input_line stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

let rec readlist n acc = match n with
	| 0 -> acc
	| _ -> readlist (n -1) (gr() :: acc);;
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;

let rec is_sq a n =
    (* let _ = begin print_int a; print_string "\n" end in *)
		if n < 0 then false
		else if n==0 || n==1 then true
    else if a*a == n then true
    else if a*a>n then false
    else
      let da = (n - a*a)/(3*a) in
      let da = if da > a then a else da in
      let da = if da < 1 then 1 else da in
      let a1 = a+da in
      is_sq a1 n;;

let rec max_list mx l = match l with
	| [] -> mx
	| h :: t ->
		let nmx = max mx h in
		max_list nmx t;;

let main() =
	let n = gr() in
	let l = readlist (n) [] in
	let lnsq = List.filter (fun x -> (not (is_sq 1 x))) l in
	let ans = max_list (-100011122) lnsq in
	begin
		print_int ans;
		print_string "\n"
	end;;

main();;

let get_int () = Scanf.scanf " %d" (fun i -> i)
let get_float () = Scanf.scanf " %f" (fun i -> i)

let is_sqrt x =
  let r = int_of_float @@ sqrt (float_of_int x) in
  r * r = x

let get_array n =
  let rec aux acc k =
    if k = 0 then acc
    else aux (get_int () :: acc) (k - 1)
  in aux [] n

let main () =
  let n = get_int () in
  let rec aux m k =
    if k = 0 then m
    else
      let x = get_int () in
      if not (is_sqrt x) && x > m then aux x (k - 1)
      else aux m (k - 1)
  in
  let ans = aux 0 n in
  Printf.printf "%d" ans

let () = main ()


Once little Vasya read an article in a magazine on how to make beautiful handmade garland from colored paper. Vasya immediately went to the store and bought n colored sheets of paper, the area of each sheet is 1 square meter.The garland must consist of exactly m pieces of colored paper of arbitrary area, each piece should be of a certain color. To make the garland, Vasya can arbitrarily cut his existing colored sheets into pieces. Vasya is not obliged to use all the sheets to make the garland.Vasya wants the garland to be as attractive as possible, so he wants to maximize the total area of m pieces of paper in the garland. Calculate what the maximum total area of the pieces of paper in the garland Vasya can get.

The first line contains a non-empty sequence of n (1 ≤ n ≤ 1000) small English letters ("a"..."z"). Each letter means that Vasya has a sheet of paper of the corresponding color. The second line contains a non-empty sequence of m (1 ≤ m ≤ 1000) small English letters that correspond to the colors of the pieces of paper in the garland that Vasya wants to make.

Print an integer that is the maximum possible total area of the pieces of paper in the garland Vasya wants to get or -1, if it is impossible to make the garland from the sheets he's got. It is guaranteed that the answer is always an integer.

NoteIn the first test sample Vasya can make an garland of area 6: he can use both sheets of color b, three (but not four) sheets of color a and cut a single sheet of color c in three, for example, equal pieces. Vasya can use the resulting pieces to make a garland of area 6.In the second test sample Vasya cannot make a garland at all — he doesn't have a sheet of color z.

let s=read_line();;
let t=read_line();;
let tab=Array.make 26 0;;
for i=0 to String.length(t)-1 do
   let n=int_of_char(t.[i])-int_of_char 'a' in
   tab.(n)<-tab.(n)+1
done;;
let tabz=Array.make 26 0;;
for i=0 to String.length(s)-1 do
   let n=int_of_char(s.[i])-int_of_char 'a' in
   tabz.(n)<-tabz.(n)+1
done;;
let taba=Array.make 26 0;;
for i=0 to 25 do
   taba.(i)<-min tab.(i) tabz.(i)
done
let b=ref true;;
for i=0 to 25 do
   if (tab.(i)<>0)&&(tabz.(i)=0) then b:=false
done;;
let m=ref 0;;
for i=0 to 25 do
   m:= !m+taba.(i)
done;;

if !b then print_int !m else print_int (-1);;

let s=read_line();;
let t=read_line();;
let tab=Array.make 26 0;;
for i=0 to String.length(t)-1 do
   let n=int_of_char(t.[i])-int_of_char 'a' in
   tab.(n)<-tab.(n)+1
done;;

for i=0 to String.length(s)-1 do
   let n=int_of_char(s.[i])-int_of_char 'a' in
   tab.(n)<-tab.(n)-1
done;;

let m=ref 0;;
for i=0 to 25 do
   m:= !m+max 0 (tab.(i))
done;;

m:=String.length(t)- !m;;
if !m=0 then m:=(-1);;
if String.length(t)=0 then m:=0;;
print_int !m;;

let s=read_line();;
let t=read_line();;
let tab=Array.make 26 0;;
for i=0 to String.length(t)-1 do
   let n=int_of_char(t.[i])-int_of_char 'a' in
   tab.(n)<-tab.(n)+1
done;;

for i=0 to String.length(s)-1 do
   let n=int_of_char(s.[i])-int_of_char 'a' in
   tab.(n)<-tab.(n)-1
done;;

let m=ref 0;;
for i=0 to 25 do
   m:= !m+max 0 (tab.(i))
done;;

m:=String.length(t)- !m;;
if !m=0 then m:=(-1);;
print_int !m;;


One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.These events are given as a sequence of strings "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted name2" user "name1" didn't have the joke in his feed yet, and "name2" already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp" and initially the joke was only in his feed.Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.

The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1 reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive. We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.

Print a single integer — the maximum length of a repost chain.

5
tourist reposted Polycarp
Petr reposted Tourist
WJMZBMR reposted Petr
sdya reposted wjmzbmr
vepifanov reposted sdya

6
Mike reposted Polycarp
Max reposted Polycarp
EveryOne reposted Polycarp
111 reposted Polycarp
VkCup reposted Polycarp
Codeforces reposted Polycarp

type repost = Leaf of string
            | Node of repost list

let rec split num str =
  match str with
  | "" -> []
  | s ->
    let (beg,rest) = (String.sub str 0 num, String.sub str num(String.length str - num)) in
    beg :: (split num rest)

let rec returnStringList strList one =
  match strList with
  | h :: t -> if (compare h " " == 0 || compare h "\n" == 0) then
    (String.lowercase one) :: (returnStringList t "")
    else returnStringList t (one^h)
  | _ -> (String.lowercase one) :: []

let splitToList line =
  let lineList = split 1 line in
  returnStringList lineList ""

let rec makePairList strList =
  match strList with
  | (a :: b :: c::[]) :: t -> (c,a) :: (makePairList t)
  | _ -> []

let makeNode pairList st =
  let rec nodePair pList str =
    match pList with
    | (a,b) :: t ->
      if (compare a str) == 0 then
        Node (Leaf(b) :: nodePair pairList b) :: nodePair t str
      else nodePair t str
    | _ -> [] in
  Node(Leaf(st) :: nodePair pairList st) :: []

let rec countNode repost num=
  match repost with
  | Node(h :: t) :: tail ->
    let n = countNode t (num+1) in
    let m = countNode tail num in
    if (n > m) then n else m
  | _ -> num

let printLeaf leaf =
  match leaf with
  | Leaf(a) -> print_string "[Leaf : "; print_string a; print_string " ]"
  | _ -> print_string " "

let rec printList strList =
  match strList with
  | h :: t ->
    print_string h;print_string " ";printList t
  | _ -> print_string "\n"
let rec printPairList pairList =
  match pairList with
  | (a,b) :: t ->
    print_string a; print_string " "; print_string b; print_string "\n";
    printPairList t
  | _ -> print_string"\n"

let rec printRepost repost =
  match repost with
  | Node (h :: t) :: tail ->
    print_string "[Node : ";printLeaf h; printRepost t; print_string " ]";
    printRepost tail
  | _ -> print_string " "

let n = read_int()

let rec strList num =
  if (num > 0) then
    let line = read_line() in
    let lineList = splitToList line in
    lineList :: (strList (num-1))
  else []
let pairList = makePairList (strList n)

let repost = makeNode pairList "polycarp"
let _ = print_int (countNode repost 0)

type graph = Leaf of string | Node of graph list ;;
let tl = read_int() ;;
let rec genList num =
        if (num > 0) then
                let line = read_line() in
                let edge = Str.split (Str.regexp " ") (String.lowercase line) in
                ((List.nth edge 2), (List.nth edge 0)) :: (genList (num-1))
        else []

let to_graph list_in str1 =
        let rec gen_pair list_in2 str2 =
                match list_in2 with
                | (a,b) :: t ->
                                if (compare a str2) == 0 then
                                        Node (Leaf(b) :: gen_pair list_in2 b) :: gen_pair t str2
                                else gen_pair t str2
                | _ -> [] in
        Node(Leaf(str1) :: gen_pair list_in str1) :: []

let rec count_node graph n =
        match graph with
        | Node(h :: t) :: tail ->
                        max (count_node t (n+1)) (count_node tail n)
        | _ -> n
let myList = genList tl ;;
let graphRep = to_graph myList "polycarp" ;;
print_int(count_node graphRep 0)


DZY loves colors, and he enjoys painting.On a colorful day, DZY gets a colorful ribbon, which consists of n units (they are numbered from 1 to n from left to right). The color of the i-th unit of the ribbon is i at first. It is colorful enough, but we still consider that the colorfulness of each unit is 0 at first.DZY loves painting, we know. He takes up a paintbrush with color x and uses it to draw a line on the ribbon. In such a case some contiguous units are painted. Imagine that the color of unit i currently is y. When it is painted by this paintbrush, the color of the unit becomes x, and the colorfulness of the unit increases by |x - y|.DZY wants to perform m operations, each operation can be one of the following:  Paint all the units with numbers between l and r (both inclusive) with color x.  Ask the sum of colorfulness of the units between l and r (both inclusive). Can you help DZY?

The first line contains two space-separated integers n, m (1 ≤ n, m ≤ 105). Each of the next m lines begins with a integer type (1 ≤ type ≤ 2), which represents the type of this operation. If type = 1, there will be 3 more integers l, r, x (1 ≤ l ≤ r ≤ n; 1 ≤ x ≤ 108) in this line, describing an operation 1. If type = 2, there will be 2 more integers l, r (1 ≤ l ≤ r ≤ n) in this line, describing an operation 2.

For each operation 2, print a line containing the answer — sum of colorfulness.

NoteIn the first sample, the color of each unit is initially [1, 2, 3], and the colorfulness is [0, 0, 0].After the first operation, colors become [4, 4, 3], colorfulness become [3, 2, 0].After the second operation, colors become [4, 5, 5], colorfulness become [3, 3, 2].So the answer to the only operation of type 2 is 8.

(*------------segment tree to do range increments and range sums------------------*)

module type S = sig
  val inc_to : int -> int64 -> unit
  val sum_to : int -> int64
  val inc_range : int -> int -> int64 -> unit
  val sum_range : int -> int -> int64
end

module Make_sumtree (M: (sig val size : int end)) : S = struct
  let ( ** ) a b = Int64.mul a b
  let ( ++ ) a b = Int64.add a b
  let ( -- ) a b = Int64.sub a b

  let super_ceiling n =
    let rec log x = if x<=1 then 0 else 1 + (log (x lsr 1)) in
    1 lsl (log (2*n-1))

  let n = M.size
  let sn = super_ceiling n
  let nn = n + sn

  let delta = Array.make nn 0L
  (* The value of a leaf is the sum of the deltas along the path from the leaf
     to the root *)
  let sum = Array.make nn 0L
  (* sum.(i) is the sum of all the leaves in the subtree rooted at i, taking into
     account all the deltas in the subtree rooted at i, but none of the deltas
     above i.*)

  let inc_to i d =
    (* s=size of the set of interest below node j
       p=power of 2 (size of left sibling of j if it has one)
    *)
    let rec loop j s p = if j>1 then
        let k = j/2 in
        if 2*k = j then (
          sum.(k) <- sum.(k) ++ s**d;
          loop k s (p++p)
        ) else (
          delta.(2*k) <- delta.(2*k) ++ d;
          sum.(2*k) <- sum.(2*k) ++ p**d;
          sum.(k) <- sum.(k) ++ (s++p)**d;
          loop k (s++p) (p++p)
        )
    in
    if i<0 then () else
      let i = i+sn in
      delta.(i) <- delta.(i) ++ d;
      sum.(i) <- sum.(i) ++ d;
      loop i 1L 1L

  let sum_to i =
    (* s=size of the set of interest below node j
       p=power of 2 (size of left sibling of j if it has one)
       ac = sum of the accumulated sum so far
    *)
    let rec loop j s p ac = if j=1 then ac else
        let k = j/2 in
        if 2*k = j then loop k s (p++p) (ac ++ delta.(k) ** s)
        else loop k (s++p) (p++p) (ac ++ sum.(2*k) ++ delta.(k) ** (s++p))
    in
    if i<0 then 0L else
      let i = i+sn in
      loop i 1L 1L sum.(i)

  let inc_range i j d = (
    inc_to j d; inc_to (i-1) (0L--d)
  )

  let sum_range i j = (sum_to j) -- (sum_to (i-1))
end

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

open Printf
open Scanf

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

let n = read_int()
let m = read_int()
module Sum = Make_sumtree (struct let size=n end)

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

let () =
  let process_recolor s l r x =
    let (s1,_,_) = Tset.split (l,n,0) s in
    let (s2,_,_) = Tset.split (r,n,0) s in
    let e_l = Tset.max_elt s1 in
    let e_r = Tset.max_elt s2 in
    let (left1,_,right2) = Tset.split e_l s in
    let (_,_,right1) = Tset.split e_r s in
    if e_l < e_r then (
      let (left3,_,_) = Tset.split e_r right2 in
      Tset.iter (
	fun (l2, r2, x2) -> Sum.inc_range l2 r2 (long (abs (x2-x)))
      ) left3;
      let (e_ll,e_lr,e_lx) = e_l in
      let (e_rl,e_rr,e_rx) = e_r in
      Sum.inc_range l e_lr (long (abs (e_lx-x)));
      Sum.inc_range e_rl r (long (abs (e_rx-x)));
      let left4 = if l>e_ll then Tset.add (e_ll,l-1,e_lx) left1 else left1 in
      let right4= if r<e_rr then Tset.add (r+1,e_rr,e_rx) right1 else right1 in
      let left5 = Tset.add (l,r,x) left4 in
      Tset.union left5 right4
    ) else ( (* both l and r are in the same block *)
      let (e_ll,e_lr,e_lx) = e_l in
      Sum.inc_range l r (long (abs (e_lx-x)));
      let s1 = Tset.remove e_l s in
      let s2 = if l>e_ll then Tset.add (e_ll,l-1,e_lx) s1 else s1 in
      let s3 = if r<e_lr then Tset.add (r+1,e_lr,e_lx) s2 else s2 in
      Tset.add (l,r,x) s3
    )
  in
 (*
  let print_set s =
    printf "set = ";
    Tset.iter (fun (l,r,x) -> printf "[%d,%d,%d] " l r x) s;
    print_newline()
  in
 *)
  let rec loop s i = if i=m then () else
      let t = read_int() in
      let l = read_int() -1 in
      let r = read_int() -1 in
      if t=1 then
	let x = read_int() in
	let s = process_recolor s l r x in
(*	printf "After (%d %d %d %d): " t l r x;
	print_set s; *)
	loop s (i+1)
      else (
	printf "%Ld\n" (Sum.sum_range l r);
	loop s (i+1)
      )
  in

  let rec build_init s i = if i=n then s else
      build_init (Tset.add (i,i,i+1) s) (i+1)
  in

  let s = build_init Tset.empty 0 in
(*  printf "initial set:";
  print_set s;*)

  loop s 0