cswork / OAOpen practice workspace

IBMOA #58

Minimum Insertions to Balance a Parentheses String

Estimated MediumJudge-ready

Topics (AI-labelled)Stacks & HeapsStrings

Only insertions of left and right parentheses are allowed, to turn the string into an ordinary valid parenthesis string. Each left parenthesis is matched with one right parenthesis. Find the minimum number of insertions.

Input

One line containing a string of only left and right parentheses, of length 1..100000.

Output

Output one integer, the answer.

Example 1

Input:

()))

Output:

2

Example 2

Input:

(()))

Output:

1

Example 3

Input:

))((

Output:

4

Example notes

The examples give 2, 1 and 4 respectively. The 4 in the original second example actually belongs to a different string; this site corrects it according to the statement body and the explanation.

Editorial

Loading solution…