# Practice on Toph

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Participate in exhilarating programming contests, solve unique algorithm and data structure challenges and be a part of an awesome community.

Sofdor Ali has invented a new game. He asked his assistant Jong Bahadur ( who happens to be a monkey ) to play the game.

Here is how the game is played:

Step 1: Sofdor Ali writes a sequence of brackets on a chalkboard.

Step 2: Jong Bahadur finds the lexicographically smallest regular bracket sequence ( if there is any ) that can be obtained by applying cyclic shift to the initial sequence.

Sofdor Ali and Jong Bahadur went on a vacation. But they left the bracket sequences behind so that we could play with them. For this task, we will give you the sequence of brackets written by Sofdor Ali and you have to tell us the sequence found by Jong Bahadur.

A regular sequence of brackets is defined as follows:

An empty string is a regular sequence of brackets.

If a string

**a**is a regular sequence of brackets, then the string**(a)**is also a regular sequence of brackets.If strings

**a**and**b**are regular sequences of brackets, then the string**ab**is also a regular sequence of brackets. There are no other regular sequences of brackets.

Also notice that,

A string

**a**is a cyclic shift of a string**b**if**a**and**b**have the same lengths and**a**consists of some (possibly empty) suffix from**b**followed by a prefix from**b**.If

**a**and**b**are two bracket sequences of the same length,**a**is lexicographically smaller than**b**if, for some**i <= |a|**,**a**for all_{j}= b_{j}**j < i**and**a**._{i}= ‘(’, b_{i}= ‘)’

The only line of input contains the sequence of brackets written by Sofdor Ali. The sequence is non-empty and its length doesn’t exceed **10 ^{5}**.

For 30% of the cases, the length of the sequence will not exceed **10 ^{3}**.

Output the lexicographically smallest regular bracket sequence that can be obtained by applying cyclic shift to the initial sequence. If there is no such sequence, output **”-1”** (without the quotation marks).

Input | Output |
---|---|

)()(() | (())() |

Input | Output |
---|---|

(( | -1 |

For the first case, there were two possible cyclic shifts that obtained a regular sequence, ie: ()(()) and (())(). Among these two, (())() is lexicographically smaller.

For the second case, we have no cyclic shift that gets us a regular bracket sequence.

51% Solution Ratio

sumit93Earliest,

QuwsarOhiFastest, 0.1s

ruhanhabib39Lightest, 524 kB

ruhanhabib39Shortest, 1441B

Login to submit