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
55
56
57
58
59
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
vector<vector<ll>> contr;
vector<vector<vector<ll>>> memo;
ll n, k;
const ll INF = 1e15;

ll dp(int l, int r, int k)
{
	if (r == n)
	{
		if (l != r) return contr[l][r-1];
		return 0ll;
	}
	else if (k < 0) return INF;
	if (memo[l][r][k] != -1)
	{
		return memo[l][r][k];
	}
	ll res = min(contr[l][r] + dp(r+1,r+1,k-1), dp(l, r+1, k));
	return memo[l][r][k] = res;
}

int main()
{
	cin >> n >> k;
	string in;
	cin >> in;
	contr.resize(n);
	memo.resize(n);
	for (int i = 0; i < n; i++)
	{
		memo[i].resize(n);
		for (int j = 0; j < n; j++)
		{
			memo[i][j].assign(k, -1);
		}
	}
	for (int i = 0; i < n; i++)
	{
		contr[i].resize(n);
		contr[i][i] = 0;
		for (int j = i + 1; j < n; j++)
		{
			ll depth = 0;
			for (int k = j; k >= i; k--)
			{
				if (in[k] == ')') depth++;
				else depth--;
				if (depth < 0) break;
				else if (depth == 0) contr[i][j]++;
			}
			contr[i][j] += contr[i][j-1];
		}
	}
	cout << dp(0, 0, k-1) << '\n';
}