QOJ.ac

QOJ

時間限制: 3 s 記憶體限制: 128 MB 總分: 100

#5262. Shallow parentheses

统计

A sequence consisting of opening and closing parentheses is called a parenthesis sequence. A parenthesis sequence is correct if the parentheses can be paired such that they are correctly nested. We also define the nesting depth.

Formally, a correct parenthesis sequence can be defined recursively: The empty sequence is a correct parenthesis sequence; its depth is 0. If $w$ is a correct parenthesis sequence of depth $h$, then the sequence $(w)$, formed by adding an opening parenthesis at the beginning and a closing parenthesis at the end, is a correct parenthesis sequence of depth $h + 1$. * If $w_1$ and $w_2$ are correct parenthesis sequences of depths $h_1$ and $h_2$ respectively, then the sequence $w_1w_2$, formed by concatenating them, is a correct parenthesis sequence of depth $\max(h_1, h_2)$.

You are given a correct parenthesis sequence $w$ and a number $H$. By a parenthesis flip, we mean changing an opening parenthesis to a closing one or vice versa. What is the minimum number of parenthesis flips required to obtain a correct parenthesis sequence with a depth no greater than $H$?

Input

The first line of input contains two integers $n$ and $H$ ($n \ge 2$, $1 \le H \le \frac{n}{2}$), representing the length of the sequence and the maximum depth.

The second line contains an $n$-character string consisting of the characters ( and ), which is a correct parenthesis sequence.

Output

Your program should output a single integer representing the minimum number of parenthesis flips required to obtain a correct parenthesis sequence with a depth of at most $H$.

Examples

Input 1

8 2
(()(()))

Output 1

2

Note

The sequence (()(())) has a depth of 3. Flipping the fifth and sixth parentheses gives the sequence (()()()) with a depth of 2. A single flip is not enough, as it would not result in a correct parenthesis sequence.

Subtasks

The test set is divided into the following subtasks. Tests for each subtask consist of one or more separate test groups. Let $h$ denote the depth of the parenthesis sequence given in the input.

Subtask Conditions Points
1 $n \le 20$ 20
2 $n \le 3000$ 40
3 $n \le 1\,000\,000$ and $H = h - 1$ 20
4 $n \le 1\,000\,000$ 20

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.