A courier service uses a special labeling system for packages nested inside one another: each package gets an opening tag when it's placed inside another, and a matching closing tag when its own contents are sealed, forming a properly nested sequence (much like matched parentheses).
Given an integer
The resulting list of sequences must be sorted in lexicographically increasing order (treating an opening tag
A single line containing the integer KaTeX can only parse string typed expression.
Print a single integer on the first line — the number of valid tag sequences. Then print that many lines, each containing one valid sequence, sorted lexicographically.
Input
3Output5
((()))
(()())
(())()
()(())
()()()ExplanationThere are exactly 5 distinct ways to properly nest 3 pairs of tags, listed in sorted order.
Example 2:
Input
1Output1
()ExplanationWith only 1 pair, there is exactly one valid arrangement.