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
222
223
224
225
226
227
228
229
230
231
#include <chrono>
#include <cassert>
#include <string>
#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
#include <random>
#include <ios>
#include <iostream>
#include <bitset>
using namespace std; 

#define fwd(i, a, b) for (int i = (a); i < (b); ++ i)
#define rep(i, n) for (int i = 0; i < (n); ++ i)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)x.size())
#define st first
#define nd second
#define pii pair<int, int>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define vi vector<int>
#define fastio ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long

#ifdef LOCAL
template<typename Stream, typename T1, typename T2>
Stream& operator << (Stream& out, pair<T1, T2> a) {return out << "(" << a.st << ", " << a.nd << ")";}
 
template<typename Stream, typename T>
Stream &operator << (Stream& out, T v) {
	out << "{";
	int i = 0;
	for (auto x : v)
		out << x << ((++ i) != sz(v) ? ", " : "");
	return out << "}";
}

template<typename... Args>
void dump(Args... x) {((cerr << x << ", "), ...) << '\n';}
 
#define debug(x...) cerr << "[" #x "]: ", dump(x)
#else
#define debug(...) 0
#endif

const int maxN = 100 * 1000;
const ll inf = 2e10;

int h[maxN + 1];
int id[maxN + 1];
int prv[maxN + 1];
int ktury[maxN + 1];

string s;

struct hull {
	int p;
	ll offset;
	vector<pair<pll, int> > cht;
	hull() {
		p = 0;
		offset = 0;
	}
	pli eval(int ps, ll x) {
		return make_pair(offset + cht[ps].st.st * x + cht[ps].st.nd, cht[ps].nd);
	}
	bool should_pop(ll slope, ll delta, int k) {
		if (sz(cht) == 0)
			return false;
		if (eval(sz(cht) - 1, 0) >= make_pair(offset + delta, k))
			return true;
		if (sz(cht) == 1)
			return false;
		ll lx = (cht.back().st.nd - cht[sz(cht) - 2].st.nd) / (cht[sz(cht) - 2].st.st - cht.back().st.st);
		while (eval(sz(cht) - 1, lx) >= eval(sz(cht) - 2, lx))
			++ lx;
		ll rx = (delta - cht.back().st.nd) / (cht.back().st.st - slope);
		while (make_pair(offset + slope * rx + delta, k) > eval(sz(cht) - 1, rx))
			++ rx;
		if (lx >= rx)
			return true;
		return false;
	}
	void insert(ll slope, ll delta, int k) {
		delta -= offset;
		while (should_pop(slope, delta, k))
			cht.pop_back();
		if (p >= sz(cht))
			p = sz(cht) - 1;
		if (p == -1)
			p = 0;
		cht.push_back(make_pair(make_pair(slope, delta), k));
	}
	void modify(ll off) {
		offset += 2 * off;
	}
	pli query(ll x) {
		while (p + 1 < sz(cht) && eval(p + 1, x) <= eval(p, x))
			++ p;
		pli odp = eval(p, x);
		odp.st += x * x + x;
		odp.st /= 2ll;
		return odp;
	}
};

hull hls[maxN + 1];

pli amogus(int n, long long lambda) {
	vector<pli> dp(n + 1);
	vector<pair<pair<pli, pli>, pli> > q;
	dp[0] = make_pair(0, 0);
	ll offset = 0;
	q.push_back(make_pair(make_pair(dp[0], dp[0]), make_pair(0, h[0])));
	rep(i, n + 1)
		hls[i] = hull();
	hls[h[0]].insert(id[0], 0, 0);
	fwd(i, 1, n + 1) {
		if (h[i] < h[i - 1]) {
			pli min_seg = hls[h[i - 1]].query(id[i - 1]);
			min_seg.st -= offset;
			hls[h[i - 1]] = hull();
			ll invs = 0;
			ll invs2 = 0;
			int zioms = 0;
			while (sz(q) && q.back().nd.nd > h[i]) {
				invs += zioms;
				invs2 += zioms;
				++ zioms;
				invs += q.back().nd.st;
				q.pop_back();
			}
			min_seg.st -= invs2;
			offset += invs2;
			pli min_pref = min_seg;
			dp[i] = make_pair(min_seg.st + lambda, min_seg.nd + 1);
			if (sz(hls[h[i]].cht) > 0) {
				hls[h[i]].modify(invs);
				pli min_pref2 = hls[h[i]].query(id[i]);
				min_pref2.st -= offset;
				min_pref = min(min_pref, min_pref2);
				dp[i] = min(dp[i], make_pair(min_pref2.st + lambda, min_pref2.nd + 1));
			}
			ll my_invs = 1ll * (id[i] * (id[i] + 1ll)) / 2ll;
			if (prv[i] != -1) {
				pli min_pref3 = make_pair(q[ktury[prv[i]]].st.st.st + my_invs, q[ktury[prv[i]]].st.st.nd);
				min_pref = min(min_pref, min_pref3);
				dp[i] = min(dp[i], make_pair(min_pref3.st + lambda, min_pref3.nd + 1));
			}
			min_pref = min(min_pref, dp[i]);
			min_seg = min(min_seg, dp[i]);
			q.push_back(make_pair(make_pair(min_pref, min_seg), make_pair(invs, h[i])));
			ktury[i] = sz(q) - 1;
			hls[h[i]].insert(-2ll * id[i], offset * 2ll + 2ll * min_seg.st + 1ll * id[i] * id[i] - id[i], min_seg.nd);
		} else if (h[i] > h[i - 1]) {
			dp[i] = make_pair(q.back().st.st.st + lambda, q.back().st.st.nd + 1);
			pli min_pref = min(dp[i], q.back().st.st);
			pli min_seg = dp[i];
			q.push_back(make_pair(make_pair(min_pref, min_seg), make_pair(0, h[i])));
			ktury[i] = sz(q) - 1;
			hls[h[i]].insert(-2ll * id[i], offset * 2ll + 2ll * min_seg.st + 1ll * id[i] * id[i] - id[i], min_seg.nd);
		}
	}
	return make_pair(dp.back().st + offset, dp.back().nd);
}

void prep(int n) {
	rep(i, n + 1)
		hls[i] = hull();
	int mn = 0;
	fwd(i, 1, n + 1) {
		h[i] = h[i - 1];
		if (s[i - 1] == '(')
			++ h[i];
		else
			-- h[i];
		if (h[i] < mn)
			mn = h[i];
	}
	vi cnt(n + 1, 0);
	vi stck;
	rep(i, n + 1) {
		h[i] -= mn;
		while (sz(stck) && h[stck.back()] >= h[i]) {
			if (h[stck.back()] > h[i])
				cnt[h[stck.back()]] = 0;
			stck.pop_back();
		}
		if (sz(stck))
			prv[i] = stck.back();
		else
			prv[i] = -1;
		stck.push_back(i);
		id[i] = cnt[h[i]] ++;
	}
}

int32_t main() {
	fastio;
	int n, k;
	cin >> n >> k;
	cin >> s;
	prep(n);
	ll l = 0, r = inf;
	ll lambda = 0;
	ll ans = 0;
	while (l <= r) {
		ll m = (l + r) / 2ll;
		pli odp = amogus(n, m);
		if (odp.nd <= k) {
			lambda = m;
			ans = odp.st;
			r = m - 1ll;
		} else
			l = m + 1ll;
	}
	ans -= lambda * k;
	cout << ans << '\n';
	return 0;
}