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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

string s;

void makeCombiUtil(vector<vector<int> >& ans, vector<int>& tmp, int n, int left, int k){
	if (k == 0){
		ans.push_back(tmp);
		return;
	}
	for (int i = left; i <= n; ++i){
		tmp.push_back(i);
		makeCombiUtil(ans, tmp, n, i + 1, k - 1);
		tmp.pop_back();
	}
}

vector<vector<int> > makeCombi(int n, int k){
	vector<vector<int> > ans;
	vector<int> tmp;
	makeCombiUtil(ans, tmp, n, 1, k);
	return ans;
}
vector<vector<int> > ans;

int calc(string s){
	struct xxx{
		int poczatek;
		int koniec;
		int ile;
	};
	int res=0;
	stack<xxx> v;
	stack<int> open;
	for(int i=0; i<int(s.size()); i++){
		if(s[i] == '('){
			open.push(i);
		}else if(!open.empty()){
			int tmp = open.top();
			open.pop();
			int ile = 1;
			while(!v.empty() && v.top().koniec > tmp){
				v.pop();
			}
			if(!v.empty() && v.top().koniec == tmp-1){
				ile += v.top().ile;
			}
			xxx new_pair = {tmp, i, ile};
			res+=ile;
			v.push(new_pair);
		}
	}
	return res;
}

int check(){
	int res = INT_MAX;
	for(auto v : ans){
		int mini=0;
		int prev=0;
		v.push_back(s.size());
		for(int x : v){
			mini += calc(s.substr(prev, x-prev));
			prev = x;
		}
		res = min(res, mini);
	}
	return res;
}

int main(){
	ios_base::sync_with_stdio(false);
	int n, k;
	cin>>n>>k>>s;
	ans = makeCombi(n-1, k-1);
	cout<<check()<<"\n";	
	return 0;
}