#C10308. Balance Parentheses

    ID: 39499 Type: Default 1000ms 256MiB

Balance Parentheses

Balance Parentheses

You are given a string s consisting solely of the characters '(' and ')'. Your task is to determine the minimum number of parentheses that must be added in order to make the string balanced.

A balanced parentheses string is defined such that every opening parenthesis '(' has a corresponding closing parenthesis ')' in the correct order. Mathematically, if we let open_count be the number of unmatched '(' and close_count be the number of unmatched ')', then the answer is given by:

result=open_count+close_countresult = open\_count + close\_count

For example:

  • For s = "(()", one ')' is needed.
  • For s = ")))(((", three ')'s and three '('s (in order) are missing, so the answer is 6.
  • For s = "()()", the string is already balanced, so the answer is 0.
  • For s = ")()(", the answer is 2.

inputFormat

The input consists of a single line string s containing only the characters '(' and ')'.

outputFormat

Output a single integer representing the minimum number of parentheses that must be added to make s balanced.

## sample
(()
1

</p>