open System.Collections let s (a:BitArray) = seq [ for i in 0..(a.Length-1) -> a.Get i ] let inline (@>>) (a:BitArray) n = Seq.append (seq[for i in 1..n -> false]) (s a) |> Seq.take a.Length |> Seq.toArray |> BitArray let inline (@<<) (a:BitArray) n = Seq.append (s a |> Seq.skip n) (seq[for i in 1..n -> false]) |> Seq.take a.Length |> Seq.toArray |> BitArray let inline (@=) (a:BitArray) (b:BitArray) = if a.Length = b.Length then Seq.zip (s a) (s b) |> Seq.forall (fun (a:bool,b:bool) -> a = b) else false let inline (@&) (a:BitArray) (b:BitArray) = let c = new BitArray(a) in c.And(b) let inline (@|) (a:BitArray) (b:BitArray) = let c = new BitArray(a) in c.Or(b) let inline (@^) (a:BitArray) (b:BitArray) = let c = new BitArray(a) in c.Xor(b) let inline (@!) (a:BitArray) = let c = new BitArray(a) in c.Not() let ``No.22 括弧の対応`` () = let (N,K) = let t = stdin.ReadLine().Split(' ') in int t.[0],int t.[1] let cnt (a:BitArray) = a |> Seq.cast |> Seq.map (fun b -> if b then 1 else 0) |> Seq.sum let trace s (b:BitArray) = b |> Seq.cast |> Seq.map (fun b -> if b then "1" else "0") |> Seq.reduce(+) |> printfn "%s: %s" s let mask = BitArray([| for _ in 1..N -> true |]) let T = BitArray([| for i in 1..N -> i = K |]) let A = stdin.ReadLine() |> Seq.map ((=)'(') |> Seq.toArray |> BitArray let B = (@!) A let rec f n a b = let c,d = (a @>> n) @& b, (b @<< n) @& a if cnt a = 0 && cnt b = 0 then failwith "syntax error" elif (c @& T) @= T then K - n elif (d @& T) @= T then K + n else f (n+2) (a @^ d) (b @^ c) f 1 A B |> printfn "%d" ``No.22 括弧の対応`` ()