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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#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<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...) {}
#endif

struct info {
	double score;
	int cnt;
	info() {
		score = 0.0;
		cnt = 0;
	}
	info(double p, int q) : score(p), cnt(q) {}
	friend info operator+(info p, info q) {
		return info(p.score + q.score, p.cnt + q.cnt);
	}
	friend info operator-(info p, info q) {
		return info(p.score - q.score, p.cnt - q.cnt);
	}
	friend info operator*(info p, double q) {
		return info(p.score * q, p.cnt);
	}
	friend bool operator<(info p, info q) {
		return p.score < q.score;
	}
	friend bool operator>(info p, info q) {
		return p.score > q.score;
	}
};

struct Function {
	info a, b;
	Function() {
		a = b = info(1e18, 0);
	}
	Function(info _a, info _b) : a(_a), b(_b) {}
	info operator()(int x) {
		return (a * (double)x) + b;
	}
};

struct LiChaoTree {
	int size = 1;
	vector<Function> tree;

	LiChaoTree(int n) {
		while(size < n)
			size *= 2;
		tree.resize(size << 1);
	}

	info get_min(int x) {
		int v = x + size;
		info ans(1e18, 0);
		while(v) {
			ans = min(ans, tree[v](x));
			v >>= 1;
		}
		return ans;
	}

	void add_func(Function new_func, int v, int l, int r) {
		int m = (l + r) / 2;
		bool domin_l = tree[v](l) > new_func(l),
			 domin_m = tree[v](m) > new_func(m);
		if(domin_m)
			swap(tree[v], new_func);

		if(l == r)
			return;
		else if(domin_l == domin_m)
			add_func(new_func, v << 1 | 1, m + 1, r);
		else
			add_func(new_func, v << 1, l, m);
	}

	void add_func(Function new_func) {
		add_func(new_func, 1, 0, size - 1);
	}
};

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

	int n, k;
	cin >> n >> k;
	vector t(n, -1);
	stack<int> stc;
	REP (i, n) {
		char c;
		cin >> c;
		if (c == '(')
			stc.emplace(i);
		else {
			if (stc.empty())
				continue;
			t[stc.top()] = i;
			t[i] = stc.top();
			stc.pop();
		}
	}
	debug(t);

	auto merge = [&](vector<pair<info, info>> &p) {
		debug("MERGE");
		for (auto [a, b] : p)
			debug(a.score, b.score, b.cnt);
		if (p.empty())
			return pair(info(0.0, 0), info(0.0, 0));
		int m = ssize(p);
		vector<info> dp(m);
		LiChaoTree con(m);
		REP (i, m) {
			dp[i] = p[i].second + info((double)i * (i - 1) / 2.0, 0);
			if (i) {
				p[i].first = p[i].first + p[i - 1].first;
				dp[i] = dp[i] + p[i - 1].first;
				debug(i, dp[i].score, dp[i].cnt);
				auto xd = con.get_min(i);
				debug(xd.score, xd.cnt);
				dp[i] = min(dp[i], p[i].second + p[i - 1].first + con.get_min(i) + 
						info(((double)i * i + 2 - 3 * i) / 2.0, 0));
			}
			con.add_func(Function(info(-i, 0), info(((double)i * i + 3 * i) / 2.0, 0) +
												  dp[i] - p[i].first));
			debug(i, dp[i].score, dp[i].cnt);
		}
		info odp = info(1e18, 0);
		REP (i, m) {
			odp = min(odp, dp[i] + p[m - 1].first - p[i].first +
					info((double)(m - i - 1) * (m - i - 2) / 2.0, 0));
		}
		debug(odp.score, odp.cnt);
		return pair(p[m - 1].first + info((double)m * (m - 1) / 2.0, 0), odp);
	};

	auto simple_merge = [&](vector<pair<info, info>> &p) {
		debug("SIMPLE_MERGE");
		info norm, all;
		for (auto [a, b] : p) {
			debug(a.score, b.score, b.cnt);
			norm = norm + a;
			all = all + min(a, b);
		}
		if (all.cnt)
			return pair(norm, all);
		info ret(1e18, 1);
		for (auto [a, b] : p)
			ret = min(ret, all - min(a, b) + b);
		return pair(norm, ret);
	};

	function<pair<info, info>(int, int, double)> rek = [&](int lf, int rg, double pen) {
		debug(lf, rg);
		if (lf + 1 == rg && t[lf] == rg) {
			debug("RET", lf, rg);
			return pair(info(1.0, 0), info(pen, 1));
		}
		if (t[lf] == rg) {
			auto [sc, ans] = rek(lf + 1, rg - 1, pen);
			sc.score += 1;
			debug("RET", lf, rg);
			return pair(sc, ans);
		}

		vector<pair<info, info>> tokens, tok;
		auto wstaw = [&]() {
			if (tokens.empty())
				return;
			tok.emplace_back(merge(tokens));
			tokens.clear();
		};

		int x = lf;
		while (x < rg) {
			if (t[x] == -1) {
				wstaw();
				++x;
				continue;
			}
			tokens.emplace_back(rek(x, t[x], pen));
			x = t[x] + 1;
		}
		wstaw();

		debug("RET", lf, rg);
		if (tok.empty())
			return pair(info(0.0, 0), info(pen, 1));

		return simple_merge(tok);
	};

	--k;
	if (k == 0) {
		auto [xd, ans] = rek(0, n - 1, 0);
		cout << LL(round(xd.score)) << endl;
		return 0;
	}

	double bp = 0.0, bk = n + 1, bs;
	REP (i, 100) {
		bs = (bp + bk) / 2;
		auto [xd, ans] = rek(0, n - 1, bs);
		debug(bp, bs, bk, xd.score, ans.score, ans.cnt);
		if (ans.cnt >= k)
			bp = bs;
		else
			bk = bs;
	}
	auto [xd, ans] = rek(0, n - 1, bp);
	cout << (LL)round(ans.score - bp * k) << endl;
}