#P9805. [POI 2022/2023 R1] ply

[POI 2022/2023 R1] ply

Background

This problem is translated from POI2022~2023R1 ply.

Problem Description

Define a "valid bracket sequence" and its depth as follows:

  • The empty string is a valid bracket sequence, with depth 00.
  • If ww is a valid bracket sequence with depth hh, then (w)(w) is also a valid bracket sequence with depth h+1h+1.
  • If w1w_1 and w2w_2 are both valid bracket sequences with depths h1h_1 and h2h_2, then w1w2w_1w_2 is also a valid bracket sequence, with depth max(h1,h2)\max(h_1,h_2).

Define flipping a character as:

  • If the current character is (, change it to ).
  • If the current character is ), change it to (.

You need to flip some characters in ss so that the depth does not exceed HH. Find the minimum number of operations.

Input Format

The first line contains two integers n (2n106)n \ (2 \leq n \leq 10^6) and H (1Hn2)H \ (1 \leq H \leq \frac{n}{2}), representing s|s| and the required maximum depth after modification.

The second line contains a string ss, representing the original bracket sequence.

Output Format

Output the minimum number of modifications.

8 2
(()(()))
2

Hint

For the sample, you can modify it to (()()()), whose depth is 22.

The subtasks are as follows:

Subtask ID Special Property Score
11 n20n \leq 20 2020
22 n3000n \leq 3000 4040
33 n106n \leq 10^6 and H=h1H = h-1 2020
44 n106n \leq 10^6

Note: hh is the depth of the input bracket sequence.

In this problem, subtask 00 is the sample.

Translated by ChatGPT 5