Bracket Match
Time limit2sMemory limit512 MB
Given a lowercase string S, find the lexicographically smallest matching bracket sequence, or print -1 if none exists.
Problem
A valid bracket sequence is defined as follows.
- The empty string is a valid bracket sequence.
- If is a valid bracket sequence, then is a valid bracket sequence.
- If and are both valid bracket sequences, then their concatenation is a valid bracket sequence.
Let be a valid bracket sequence of length , and let be its -th character. Two indices and with are matching brackets when both of these hold.
- is
(and is). - , or the substring is a valid bracket sequence.
Let be a string of lowercase letters, and let be its -th character. A valid bracket sequence matches when both of these hold.
- has the same length as .
- For every pair of indices , if and are matching brackets, then .
You are given a string of lowercase letters. Find the lexicographically smallest valid bracket sequence that matches .
Input
The first line contains a string of lowercase letters.
- A bracket sequence is lexicographically smaller than a bracket sequence if there is an index () such that for every and .
- The character
(is lexicographically smaller than the character).
Output
Print the lexicographically smallest valid bracket sequence that matches . If no such sequence exists, print -1.