#C7734. Minimum Additions to Make Parentheses Valid

    ID: 51638 Type: Default 1000ms 256MiB

Minimum Additions to Make Parentheses Valid

Minimum Additions to Make Parentheses Valid

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

A parentheses string is considered valid if and only if:

  • Every opening parenthesis '(' has a corresponding closing parenthesis ')'.
  • The parentheses are correctly nested.

You can prove that the answer is equal to the sum of the number of unmatched opening and unmatched closing parentheses.

In mathematical terms, if we denote the number of unmatched opening parentheses as \(L\) and unmatched closing parentheses as \(R\), then the answer is:

[ \text{answer} = L + R ]

For example:

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

inputFormat

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

outputFormat

Output a single integer that represents the minimum number of parentheses you need to add to the string to make it valid.

## sample
())
1