CEOI '16 P4 - Match

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 2.0s
Memory limit: 512M

Problem types

We define a valid bracket sequence as a string that is either:

  • The empty string;
  • A string (B), where B is a valid bracket sequence.
  • LR, the concatenation of two strings L and R which are both valid bracket sequences.

Let B be a valid bracket sequence of length N. We define Bi to be the i-th character of sequence B. For two indices i and j, 1i<jN, we say that Bi and Bj are matching brackets if:

  • Bi= ( and Bj= );
  • i=j1, or the subsequence C=Bi+1Bi+2Bj1 is a valid bracket sequence.

Let S be a string of lowercase English letters. We define Si to be the i-th character of string S. We say that a valid bracket sequence B matches S if:

  • B has the same length as S;
  • for any pair of indices i and j,i<j, if Bi and Bj are matching brackets, then Si=Sj.

For a given string S consisting of N lowercase letters, find the lexicographically smallest valid bracket sequence that matches S, or print -1 if no such bracket sequence exists.

Input

The input contains a string S of N lowercase letters on the first line.

Output

In the output you should write either a string B with N characters that represents the lexicographically smallest bracket sequence that matches the input string, or -1 if no such bracket sequence exists.

Notes and constraints

  • 2N100000
  • For test cases worth 10 points N18.
  • For test cases worth another 27 points N2000.
  • We say that a bracket sequence A is lexicographically smaller than a bracket sequence B if there is an index i,1iN, such that Aj=Bj for each j<i, and Ai<Bi.
  • Character ( is considered lexicographically smaller than character ).

Sample Input 1

Copy
abbaaa

Sample Output 1

Copy
(()())

Note for Sample Input 1

Another valid bracket sequence is (())(), but this is not the smallest lexicographic solution.

Sample Input 2

Copy
abab

Sample Output 2

Copy
-1

Note for Sample Input 2

There is no valid bracket sequence that matches the given string.


Comments

There are no comments at the moment.