Under construction

Still being built. Things may change

Enumerating Brackets

Interview

Time limit2sMemory limit512 MB

Summary
Given N and M, print the M-th balanced bracket sequence of length N in lexicographic order, where '(' is smaller than ')'.
Level

Medium6 of 10

Topics
Combinatorics, Dynamic programming, Greedy, Implementation
Solved
No attempts yet

Problem

A balanced bracket sequence is a string made only of the characters ( and ) in which every opening bracket has a matching closing bracket, and every closing bracket has a matching opening bracket. For example, (())() is a balanced bracket sequence, while (())(() and ())(() are not.

Let AA and BB be two bracket sequences of the same length. We say that AA is lexicographically smaller than BB, written A<BA < B, when both of the following hold:

  1. AA and BB differ in at least one position.
  2. At the leftmost position where AA and BB differ, AA has a ( and BB has a ).

For example, (())()<()()()(())() < ()()(), because the two strings first differ at the second position from the left, where the first string has a ( and the second has a ).

For a fixed length NN, the relation << orders all balanced bracket sequences of length NN. For N=6N = 6 the order is:

  1. ((()))
  2. (()())
  3. (())()
  4. ()(())
  5. ()()()

Given NN and a positive integer MM, find the MM-th balanced bracket sequence of length NN in this order.

Input

The first line contains an even integer NN (2≤N≤20002 \le N \le 2000) and a positive integer MM, separated by a space. MM is at most 101810^{18}, and MM is also at most the number of balanced bracket sequences of length NN.

Output

Print the MM-th balanced bracket sequence of length NN in the order described above.

Examples7

  1. Example 1

    Input
    6 4
    
    Expected output
    ()(())
    
  2. Example 2

    Input
    2 1
    
    Expected output
    ()
    
  3. Example 3

    Input
    4 2
    
    Expected output
    ()()
    
  4. Example 4

    Input
    6 1
    
    Expected output
    ((()))
    
  5. Example 5

    Input
    6 5
    
    Expected output
    ()()()
    
  6. Example 6

    Input
    10 42
    
    Expected output
    ()()()()()
    
  7. Example 7

    Input
    8 7
    
    Expected output
    (()())()