#P1239B. The World Is Just a Programming Task (Hard Version)
The World Is Just a Programming Task (Hard Version)
No submission language available for this problem.
Description
This is a harder version of the problem. In this version, .
Vasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence.
To digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him.
We remind that bracket sequence is called correct if:
- is empty;
- is equal to "()", where is correct bracket sequence;
- is equal to , i.e. concatenation of and , where and are correct bracket sequences.
For example, "(()())", "()" are correct, while ")(" and "())" are not.
The cyclical shift of the string of length by () is a string formed by a concatenation of the last symbols of the string with the first symbols of string . For example, the cyclical shift of string "(())()" by equals "()(())".
Cyclical shifts and are considered different, if .
The first line contains an integer (), the length of the string.
The second line contains a string, consisting of exactly characters, where each of the characters is either "(" or ")".
The first line should contain a single integer — the largest beauty of the string, which can be achieved by swapping some two characters.
The second line should contain integers and () — the indices of two characters, which should be swapped in order to maximize the string's beauty.
In case there are several possible swaps, print any of them.
Input
The first line contains an integer (), the length of the string.
The second line contains a string, consisting of exactly characters, where each of the characters is either "(" or ")".
Output
The first line should contain a single integer — the largest beauty of the string, which can be achieved by swapping some two characters.
The second line should contain integers and () — the indices of two characters, which should be swapped in order to maximize the string's beauty.
In case there are several possible swaps, print any of them.
Samples
Note
In the first example, we can swap -th and -th character, obtaining a string "()()()()()". The cyclical shifts by of this string form a correct bracket sequence.
In the second example, after swapping -th and -th character, we obtain a string ")(())()()(()". The cyclical shifts by of this string form a correct bracket sequence.
In the third example, swap of any two brackets results in cyclical shifts being correct bracket sequences.