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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int n, k;
	cin >> n >> k;
	if (k == n) {
		cout << 0 << '\n';
		return 0;
	}

	vector<char> v(n);
	REP(i, n)
		cin >> v[i];
	{
		/*
		auto vin = v;
		vector<bool> used(n);
		vector<int> pos;
		REP(i, n) {
			if (vin[i] == '(') {
				pos.emplace_back(i);
			}
			else if (ssize(pos)) {
				auto x = pos.back();
				pos.pop_back();
				used[x] = true;
				used[i] = true;
			}
		}
		v.clear();
		REP(i, n) {
			if (used[i]) {
				v.emplace_back(vin[i]);
			}
		}
		n = ssize(v);
		if (n == 0 or k >= n) {
			cout << 0 << '\n';
			return 0;
		}
		*/
	}
	debug(n, k, v);

	vector<int> pref(n);
	REP(i, n) {
		if (i) {
			pref[i] = pref[i - 1];
		}
		if (v[i] == '(') {
			++pref[i];
		}
	}

	int CNT = 0;
	vector ok(n, vector (n, false));
	REP(i, n - 1) {
		if (v[i] == '(' and v[i + 1] == ')') {
			ok[i][i + 1] = true;
			++CNT;
		}
	}
	if (k > CNT) {
		cout << 0 << '\n';
		return 0;
	}
	FOR(right, 0, n - 1) {
		for (int left = right - 2; left >= 0; --left) {
			if (v[left] == ')') {
				continue;
			}
			int sum = pref[right];
			if (left > 0) {
				sum -= pref[left - 1];
			}
			if (2 * sum != right - left + 1) {
				continue;
			}
			if (ok[left + 1][right - 1] and v[left] == '(' and v[right] == ')') {
				ok[left][right] = true;
				continue;
			}
			FOR(split, left, right - 1) {
				if (ok[left][split] and ok[split + 1][right]) {
					ok[left][right] = true;
					break;
				}
			}
		}
	}
	debug(ok);
	vector cnt(n, vector(n, 0));
	FOR(right, 0, n - 1) {
		for (int left = right; left >= 0; --left) {
			if (left < right) {
				cnt[left][right] = cnt[left + 1][right] + cnt[left][right - 1] - cnt[left + 1][right - 1];
			}
			cnt[left][right] += ok[left][right];
		}
	}
	debug(cnt);

	const int INF = 1e9;
	vector dp(n + 1, vector (k + 1, INF));
	dp[0][0] = 0;
	FOR(i, 1, n) {
		if (i != n and (not (v[i - 1] == '(' and v[i] == ')'))) {
			continue;
		}
		FOR(left, 1, i) {
			if (left != 1 and (not (v[left - 1] == ')' and v[left - 2] == '('))) {
				continue;
			}
			FOR(j, 1, k) {
				dp[i][j] = min(dp[i][j], dp[left - 1][j - 1] + cnt[left - 1][i - 1]);
			}
		}
	}
	debug(dp);
	int ans = INF;
	FOR(i, 0, k) {
		ans = min(ans, dp[n][i]);
	}
	cout << ans << '\n';
}