MoneyGold Medals can’t buy happiness ~ Certainly not a programmer
Seby the little square has recently heard about the newest attraction in the Info(1)cup Fair: the bracket wheel! The wheel consists of parantheses, placed on a circular frame, that can be seen as a sequence of characters, each of which can either be (
or )
. Spinning the wheel is equivalent to transforming it into one of its cyclic shifts.
Now, we define the cyclic shifts of a sequence as follows. Given a sequence , the sequence has cyclic shifts: of form when , and one cyclic shift equal to the original sequence . For example the sequence of parentheses ()(())
has the following cyclic shifts:
()(())
)(())(
(())()
())()(
))()((
)()(()
We call a sequence of parentheses balanced if we can insert 1
and +
into the sequence so that it becomes a valid mathematical expression. For example, (())()
is balanced, since we can insert 1
and +
to form ((1 + 1) + 1) + (1 + 1)
, but )(()
or (()
are not. More formally, a sequence is balanced if and only if it is empty or of the form (b)c
where b
and c
are balanced.
Task
Given a sequence of parentheses , we define the value of , denoted by , to be the number of cyclic shifts of that are balanced. For example, if ()(())
, we have , due to the cyclic shifts ()(())
and (())()
.
The rules of the game are simple. The player is given Gold Medals. They can spend one gold medal to swap two parentheses on the wheel. The score of the player is then the number of cyclic shifts of the sequence on the wheel which are balanced. That is, if is the sequence on the wheel, then the score is . Can you help Seby spend his Gold Medals to maximise his score?
Formally, you are given a sequence of parentheses, and have the ability to swap pairs of parentheses within . Find a way to perform these swaps in order to maximize the value of the resulting sequence.
Input data
The first line of the input contains the integers and . The second line of the input contains the sequence of parentheses.
Output data
The output contains the maximum value that could be obtained after performing the swaps.
Restrictions
- contains only parentheses i.e.
(
and)
. It is guaranteed that the number of(
in is equal to the number of)
. - The number of Gold Medals Seby won is significantly bigger, but his instinct tells him he might need them again in the near future.
# | Points | Restrictions |
---|---|---|
1 | 7 | |
2 | 9 | |
3 | 13 | |
4 | 17 | |
5 | 18 | |
6 | 19 | |
7 | 17 | No further restrictions. |
Example
stdin
6 1
)(())(
stdout
3
Explanation
In this case, we can swap parantheses on positions and . The resulting sequence is )()()(
, which has the following cyclic shifts:
)()()(
()()()
)()()(
()()()
)()()(
()()()
Out of these, only are balanced sequences.