#K51757. Minimum Parentheses Balancing Operations

    ID: 29158 Type: Default 1000ms 256MiB

Minimum Parentheses Balancing Operations

Minimum Parentheses Balancing Operations

Given a string s consisting solely of the characters '(' and ')', your task is to determine the minimum number of operations required to balance the string. A string is considered balanced if every opening parenthesis has a corresponding closing parenthesis and the parentheses are in the correct order.

The operation allowed is to fix an imbalance by effectively adding a matching parenthesis for an unmatched one. In this problem, the minimum number of operations is given by the formula:

$$\text{operations} = \max(\text{unmatched}\_\text{left}, \; \text{unmatched}\_\text{right}) $$

For example:

  • For s = "())(", the answer is 1.
  • For s = ")(", the answer is 1.
  • For s = "((()))", the answer is 0.

Note that the input is provided via standard input and the output should be printed to standard output.

inputFormat

The input is given as a single line from standard input containing a non-empty string s composed only of the characters '(' and ')'.

outputFormat

Output a single integer on standard output, representing the minimum number of operations required to balance the string.

## sample
())(
1