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
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
constexpr int N = 1e5 + 10, LG = 19;
int n, k, mn, tot, s[N], pos[N], cnt[N], rnk[N], fa[N], L[N], R[N];
int dfn[N], dep[N], pre[N], lcnt[N], rcnt[N];
i64 f[N], g[N], h[N];
string str;
vector<int> G[N], vec[N];
struct ST {
	int n;
	pair<int, int> st[LG][N];
	int emplace_back(int x, int y) {return st[0][n] = {x, y}, ++ n;}
	void init() {
		for(int i = 1; i < LG; i ++) {
			for(int j = 0; j <= n; j ++) st[i][j] = min(st[i - 1][j], st[i - 1][j + (1 << i - 1)]);
		}
	}
	pair<int, int> qry(int l, int r) {
		int k = __lg(r - l + 1);
		return min(st[k][l], st[k][r - (1 << k) + 1]);
	}
}cart, euler;
struct Node {
	i64 mn;
	int cnt;
	friend bool operator < (Node x, Node y) {return x.mn < y.mn;}
	friend Node operator + (Node x, i64 y) {return {x.mn + y, x.cnt};}
	friend Node operator - (Node x, i64 y) {return {x.mn + y, x.cnt + 1};}
}dp[N];
int lca(int u, int v) {
	if(u == v) return u;
	if(dfn[u] > dfn[v]) swap(u, v);
	return euler.qry(dfn[u], dfn[v] - 1).second;
}
i64 c2(int x) {return 1ll * x * (x - 1) / 2;}
int build(int l, int r) {
	if(l > r) return 0;
	int w = cart.qry(l, r).first, u = ++ tot;
	L[u] = l, R[u] = r;
	auto &res = vec[w - mn];
	auto pl = lower_bound(res.begin(), res.end(), l), pr = upper_bound(res.begin(), res.end(), r);
	int last = l; cnt[u] = pr - pl; 
	auto setson = [&] (int pl, int pr, int sz) {
		int v = build(pl, pr);
		rnk[v] = sz;
		G[u].emplace_back(v);
		h[u] += h[v];
		pre[v] = h[u];
		fa[v] = u;
	};
	for(auto it = pl; it != pr; it ++) {
		if(*it > last) setson(last, *it - 1, it - pl);
		last = *it + 1;
		pos[*it] = u;
		lcnt[*it] = c2(it - pl + 1) + h[u];
		rcnt[*it] = c2(pr - it) - h[u];
	}
	if(r >= last) setson(last, r, pr - pl);
	for(auto it = pl; it != pr; it ++) rcnt[*it] += h[u];
	h[u] += c2(cnt[u]);

	return u;
}
void dfs(int u) {
	static int ts = 0;
	dfn[u] = euler.emplace_back(dep[fa[u]], fa[u]);
	{
		i64 cur = 0;
		reverse(G[u].begin(), G[u].end());
		for(auto v : G[u]) {
			f[v] = f[u] + c2(cnt[u] - rnk[v]) + cur;
			cur += h[v];
		}
	}
	{
		i64 cur = 0;
		reverse(G[u].begin(), G[u].end());
		for(auto v : G[u]) {
			g[v] = g[u] + c2(rnk[v]) + cur;
			cur += h[v];
		}
	}
	for(auto v : G[u]) dep[v] = dep[u] + 1, dfs(v);
}
int find(int u, int v) {
	int l = 0, r = G[u].size() - 1, ans = -1;
	while(l <= r) {
		int mid = l + r >> 1;
		if(L[G[u][mid]] <= v) ans = mid, l = mid + 1;
		else r = mid - 1;
	}
	return ans;
}
i64 calc(int l, int r) {
	if(l == r) return 0;
	int u = pos[l], v = pos[r], w = lca(u, v);
	int pl = find(w, l), pr = find(w, r);
	i64 ans = c2(rnk[G[w][pr]] - (pl >= 0 ? rnk[G[w][pl]] : 0));
	if(u != w) ans += f[u] - f[G[w][pl]] + rcnt[l];
	if(v != w) ans += g[v] - g[G[w][pr]] + lcnt[r], pr --;
	else ans += rnk[G[w][pr]] - (pl >= 0 ? rnk[G[w][pl]] : 0);
	ans += (pr >= 0 ? pre[G[w][pr]] : 0) - (pl >= 0 ? pre[G[w][pl]] : 0);
	return ans;
}
Node check(i64 val) {
	struct Range {
		int l, r, s;
		Range(int _l, int _r, int _s) : l(_l), r(_r), s(_s) {}
	};
	deque<Range> que;
	que.emplace_back(1, n, 0);
	auto get = [&] (int i, int j) {
		return dp[i] + calc(i, j);
	};
	for(int i = 1; i <= n; i ++) {
		if(que.front().l == i - 1) que.front().l ++;
		if(que.front().l > que.front().r) que.pop_front();
		dp[i] = get(que.front().s, i) - val;
		while(!que.empty() && get(i, que.back().l) < get(que.back().s, que.back().l)) que.pop_back();
		if(que.size()) {
			auto [pl, pr, s] = que.back(); que.pop_back();
			int l = pl, r = pr, ans = -1;
			while(l <= r) {
				int mid = l + r >> 1;
				if(get(i, mid) < get(s, mid)) ans = mid, r = mid - 1;
				else l = mid + 1;
			}
			if(~ans) que.emplace_back(pl, ans - 1, s), que.emplace_back(ans, n, i);
			else {
				que.emplace_back(pl, pr, s);
				if(pr != n) que.emplace_back(pr + 1, n, i);
			}
		} else {
			que.emplace_back(i + 1, n, i);
		}
	}
	return dp[n];
}
int main() {
	// freopen("in.txt", "r", stdin);
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n >> k >> str;
	for(int i = 1; i <= n; i ++) {
		s[i] = s[i - 1] + (str[i - 1] == '(' ? 1 : -1);
	}
	for(int i = 0; i <= n; i ++) {
		cart.emplace_back(s[i], i);
	}
	cart.init();
	mn = *min_element(s + 1, s + n + 1);
	for(int i = 0; i <= n; i ++) {
		vec[s[i] - mn].emplace_back(i);
	}
	int rt = build(0, n);
	dfs(rt);
	euler.init();
	i64 l = 0, r = 1e9, ans = -1;
	while(l <= r) {
		i64 mid = l + r >> 1;
		if(check(mid).cnt <= k) ans = mid, r = mid - 1;
		else l = mid + 1;
	}
	cout << check(ans).mn - k * ans << '\n';
	return 0;
}