atcoder#AGC054D. [AGC054D] (ox)

[AGC054D] (ox)

Score : 10001000 points

Problem Statement

Given is a string SS consisting of (, ), o, and x. You can swap two adjacent characters in SS any number of times. Find the minimum number of swaps needed to satisfy the following condition.

  • Let us make a string SS' consisting of ( and ) by replacing every occurrence of o with () and every occurrence of x with )( in SS. Then, SS' is a balanced parenthesis string.
Definition of a balanced parenthesis string

A balanced parenthesis string is a string that is one of the following:

  • an empty string;
  • a string that is a concatenation of ss, tt in this order, where ss and tt are some non-empty balanced parenthesis strings;
  • a string that is a concatenation of (, ss, ) in this order, where ss is some balanced parenthesis string.
Under the Constraints of this problem, it is always possible to achieve the objective.

Constraints

  • SS is a string consisting of (, ), o, and x.
  • SS contains the same non-zero number of (s and )s.
  • S8000|S| \leq 8000

Input

Input is given from Standard Input in the following format:

SS

Output

Print the answer.

)x(
3

We should do as follows: )x(x)(x()(x). Here, we have S=S'=()(), which is a balanced parenthesis string.

()ox
2
()oxo(xxx))))oox((oooxxoxo)oxo)ooo(xxx(oox(x)(x()x
68