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>.
def main():
       string = input()
       correspondingOpen = [-2]*len(string)
       earliestOpen = [-2]*len(string)
       stack = []
       for i in range(len(string)):
              if string[i] == '(':
                     stack.append(i)
              if string[i] == ')':
                     if len(stack) == 0:
                            correspondingOpen[i] = -1
                            earliestOpen[i] = -1
                     else:
                            correspondingOpen[i] = stack.pop()
                            if string[correspondingOpen[i] - 1] == ')' and earliestOpen[correspondingOpen[i] - 1] != -1:
                                   earliestOpen[i] = earliestOpen[correspondingOpen[i] - 1]
                            else:
                                   earliestOpen[i] = correspondingOpen[i]
       maxLength = -1
       countSequences = 0
       for i in range(len(string)):
              if earliestOpen[i] >= 0 and (i + 1 == len(string) or earliestOpen[i + 1] == -1):
                     countSequences += 1
                     if i - earliestOpen[i] + 1 > maxLength:
                            maxLength = i - earliestOpen[i] + 1
       if countSequences == 0:
              countSequences += 1
       if maxLength < 0:
              maxLength = 0
       print(maxLength,countSequences)
main()