Algorithm


B. Regular Bracket Sequence
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.

One day Johnny got bracket sequence. He decided to remove some of the brackets from it in order to obtain a regular bracket sequence. What is the maximum length of a regular bracket sequence which can be obtained?

Input

Input consists of a single line with non-empty string of «(» and «)» characters. Its length does not exceed 106.

Output

Output the maximum possible length of a regular bracket sequence.

Examples
input
Copy
(()))(
output
Copy
4
input
Copy
((()())
output
Copy
6



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <cstdio>
#include <iostream>


int main(){

    std::string input; getline(std::cin, input);

    long unmatched(0), matched(0);

    for(int k = 0; k < input.size(); k++){
        if(input[k] == '('){++unmatched;}
        else if(input[k] == ')' && unmatched > 0){--unmatched; ++matched;}
    }

    std::cout << 2 * matched << std::endl;

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
(()))(

Output

x
+
cmd
4
Advertisements

Demonstration


Codeforces Solution-B. Regular Bracket Sequence-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+