1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 5005;

int n, k, l, i, j, d, sum[2][N][N], cou[N][N], is[N][N], tab[N], act, pre;
char t[N];

int main() {
scanf("%d %d", &n, &k);
scanf("%s", t);
for (i=0;i<n;i++) tab[i] = (t[i] == '(');

for (i=0;i<n - 1;i++) if (tab[i] && !tab[i + 1]) {
	is[i][i + 1] = 1;
	sum[1][i][i + 1] = 1;
}
for (d=2;d<n;d++) {
	for (i=0;i<n-d;i++) if (tab[i] && !tab[i + d]) {
		if (is[i + 1][i + d - 1]) is[i][i + d] = 1; else {
			for (j=i+1;j<i+d;j++) if (is[i][j] && is[j+1][i+d]) {
				is[i][i + d] = 1;
				break;
			}
		}
	}
	for (i=0;i<n-d;i++) {
		sum[1][i][i + d] = sum[1][i][i + d - 1];
		if (!tab[i + d]) {
			for (j=i;j<i+d;j++) if (is[j][i + d]) sum[1][i][i + d]++;
		}
	}
}
act = 1;
for (l=2;l<=k;l++) {
	act = l % 2;
	pre = 1 - act;
	for (i=0;i<n;i++) for (j=i+1;j<n;j++) {
		if (l == 2) cou[i][j] = sum[pre][i][j];
		sum[act][i][j] = 0;
	}
	for (i=0;i<n;i++) for (j=i+l-1;j<n;j++) {
		sum[act][i][j] = INF;
		for (d=i+l-2;d<j;d++) sum[act][i][j] = min(sum[act][i][j], sum[pre][i][d] + cou[d + 1][j]);
		//printf("%d %d %d %d\n", i, j, l, sum[act][i][j]);
	}
}
printf("%d\n", sum[act][0][n - 1]);
return 0;
}