#C7885. Balance the Parentheses

    ID: 51805 Type: Default 1000ms 256MiB

Balance the Parentheses

Balance the Parentheses

You are given a string s consisting solely of the characters '(' and ')'. Your task is to determine the minimum number of moves required to make the string of parentheses balanced. A move is defined as inserting a single parenthesis at any position in the string.

A balanced sequence has every opening parenthesis '(' matched with a closing parenthesis ')'. For example, if s = "(()", inserting one ')' at the end will balance the sequence. Similarly, for s = ")()(", at least two insertions are required.

Note: The answer should be computed by considering unmatched closing parentheses that require an insertion of an opening parenthesis and unmatched opening parentheses that require an insertion of a closing parenthesis. Mathematically, if we let \( C_{open} \) be the count of unmatched '(' and \( C_{close} \) be the count of unmatched ')', the minimum moves required is \( C_{open} + C_{close} \).

inputFormat

A single line containing the string s composed only of characters '(' and ')'. The string may be empty.

outputFormat

An integer denoting the minimum number of moves required to balance the parentheses sequence.## sample

(()
1