This is yet another problem dealing with regular bracket sequences.

We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. 

You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 10<sup class="upper-index">6</sup>.
import sys


class BracketsStack:
    def __init__(self):
        # self._temp = ''
        self.__stack = []
        self.pairs = []
        self.cur_seq_pairs = 0
        self.has_pair = False
        self.length = 0
        self.is_regular = True
        self.max_len, self.max_len_pairs_count = 0, 0

    @property
    def is_empty(self):
        return not bool(self.__stack)

    def __len__(self):
        return len(self.__stack)

    def pop(self):
        self.__stack.pop()

    def add(self, bracket: str):
        self.length += 1
        # self._temp += bracket
        if bracket == '(':
            self.__stack.append(bracket)
        elif bracket == ')':
            if self.__stack:
                self.has_pair = True
                self.__stack.pop()
                self.cur_seq_pairs += 1
            elif self.cur_seq_pairs:
                self.pairs.append(self.cur_seq_pairs)
                self.cur_seq_pairs = 0
                self.is_regular = False

    def count_result(self):

        if self.cur_seq_pairs:
            self.pairs.append(self.cur_seq_pairs)

        if self.pairs:
            if self.is_regular and not self.__stack:
                self.max_len = sum(self.pairs)
                self.max_len_pairs_count = 1
            else:
                for pair_count in self.pairs:
                    if pair_count > self.max_len:
                        self.max_len = pair_count
                        self.max_len_pairs_count = 1
                    elif pair_count == self.max_len:
                        self.max_len_pairs_count += 1


def get_result(line: str, length: int, start_index: int = 0):
    bs = BracketsStack()
    i = start_index
    while i < length:
        s = line[i]
        if i > 0 and s == '(' and bs.has_pair and line[i - 1] == '(' and len(
                bs) > 1:
            bs.pop()
            # bs._temp = bs._temp[:-1]
            sub_max, sub_count, sub_bs = get_result(line, length, i - 1)
            i += sub_bs.length - 1
            bs.length += sub_bs.length
            if sub_bs.is_regular and sub_bs.is_empty:
                bs.cur_seq_pairs += sub_bs.max_len
            else:
                bs.pairs.extend(sub_bs.pairs)
        else:
            if s == ')' and start_index > 0 and not len(bs):
                break
            bs.add(s)
            i += 1

    bs.count_result()
    return bs.max_len, bs.max_len_pairs_count, bs


if __name__ == "__main__":
    line = next(sys.stdin)
