#C10182. Longest Valid Parentheses

    ID: 39359 Type: Default 1000ms 256MiB

Longest Valid Parentheses

Longest Valid Parentheses

Given a string s consisting only of characters '(' and ')', your task is to determine the length of the longest valid (well-formed) parentheses substring.

A valid substring is defined as a substring where the parentheses are balanced. For example, in the string "()()", the entire string is valid and its length is 4, while in "(()", the longest valid substring is "()" with a length of 2. The solution should handle edge cases properly (e.g., when the string is empty or no valid substring exists).

The algorithm should optimize performance for large inputs. Mathematical notation, if any, should be provided in LaTeX format. For instance, if one needs to denote the length, it may be written as \(\text{length}\).

inputFormat

The input is read from standard input (stdin) and consists of a single line containing a string \(s\) which only includes the characters '(' and ')'.

outputFormat

Output the length of the longest valid parentheses substring to standard output (stdout) as an integer.

## sample
()
2