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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
// #define __builtin_ctz __builtin_ctzll
// #define __builtin_clz __builtin_clzll
// #define __builtin_popcount __builtin_popcountll
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
  bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }

const int N = 2e5 + 5;
int bil[N];
int last_vis[N];
int cnt_bil[N];
#define round dupa
int round;
int Get(int i) {
  if (last_vis[i] != round) {
    last_vis[i] = round;
    cnt_bil[i] = 0;
  }
  return cnt_bil[i];
}
void Set(int i, int v) {
  last_vis[i] = round;
  cnt_bil[i] = v;
}

int n;

struct Info {
  int i;
  int range;
  int L, R;
  int highest_important;
  array<VI, 2> cnt_within; // nawiasowania zawarte w polowce
  array<VI, 2> min_bil; // minimalny bilans na prefiksie
  array<VI, 2> cnt_min; // zlicz minimalny bilans na prefiksie
  array<VI, 2> cnt_by_visible_bil; // zliczaj widoczne wystapienia bilansu
  VI enclosed;
  
  void Build(int ii) {
    i = ii;
    range = 1 << __builtin_ctz(i);
    L = i - range;
    R = min(n, i + range - 1);
    highest_important = min(bil[i], bil[i - 1]);
    REP (tr, 2) {
      int start = i;
      int step = 1;
      if (tr == 0) {
        start = i - 1;
        step = -1;
      }
      round++;
      for (int cur = start; cur >= L && cur <= R; cur += step) {
        int g = Get(bil[cur]);
        if (bil[cur] > bil[cur - step]) {
          g = 0;
          Set(bil[cur], 1);
        } else {
          Set(bil[cur], g + 1);
        }
        if (cur == start) {
          cnt_within[tr].PB(0);
          min_bil[tr].PB(bil[cur]);
          cnt_min[tr].PB(1);
        } else {
          cnt_within[tr].PB(cnt_within[tr].back() + g);
          if (bil[cur] < min_bil[tr].back()) {
            min_bil[tr].PB(bil[cur]);
            cnt_min[tr].PB(1);
          } else {
            min_bil[tr].PB(min_bil[tr].back());
            cnt_min[tr].PB(cnt_min[tr].back() + (bil[cur] == min_bil[tr].back()));
          }
        }
        if (highest_important >= bil[cur] && bil[cur] == min_bil[tr].back()) {
          int place = highest_important - bil[cur];
          if (SZ(cnt_by_visible_bil[tr]) <= place) {
            cnt_by_visible_bil[tr].PB(1);
          } else {
            cnt_by_visible_bil[tr][place]++;
          }
        }
      }
    }
    REP (j, min(SZ(cnt_by_visible_bil[0]), SZ(cnt_by_visible_bil[1]))) {
      enclosed.PB(cnt_by_visible_bil[0][j] * cnt_by_visible_bil[1][j]);
    }
    RE (j, SZ(enclosed) - 1) {
      enclosed[j] += enclosed[j - 1];
    }
  }
  int Query(int a, int b) {
    //debug(a, b, i, L, R);
    assert(a <= i - 1 && a >= L);
    assert(b >= i && b <= R);
    int within_halves = cnt_within[0][i - 1 - a] + cnt_within[1][b - i];
    int mbL = min_bil[0][i - 1 - a];
    int mbR = min_bil[1][b - i];
    int above = 0;
    int mb = max(mbL,mbR);
    int ind1 = highest_important - mb - 1;
    if (ind1 >= 0 && ind1 < SZ(enclosed)) {
      above = enclosed[ind1];
    }
    int on_min = 0;
    if (mbL == mbR) {
      on_min = cnt_min[0][i - 1 - a] * cnt_min[1][b - i];
    } else if (mbL < mbR) {
      int ind = highest_important - mbR;
      if (ind >= 0 && ind < SZ(cnt_by_visible_bil[0])) {
        on_min = cnt_min[1][b - i] * cnt_by_visible_bil[0][ind];
      }
    } else {
      int ind = highest_important - mbL;
      if (ind >= 0 && ind < SZ(cnt_by_visible_bil[1])) {
        on_min = cnt_min[0][i - 1 - a] * cnt_by_visible_bil[1][ind];
      }
    }
    //debug(within_halves, above, on_min);
    return 2 * (within_halves + above + on_min);
  }
};

Info dc[N];
int Query(int i, int j) {
  i--;
  int x = i ^ j;
  int highest_bit = 31 - __builtin_clz(x);
  int where = j & ~((1 << highest_bit) - 1);
  //debug(i, j, where);
  return dc[where].Query(i, j);
}

int dp[N];
int cnt_photos[N];
int L[N];
int R[N];
VPII evs[N];
int alive[N];
LD Timestamp() {
  return 1. * clock() / CLOCKS_PER_SEC;
}
int32_t main() {

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  
  int k;
  cin>>n>>k;
  vector<char> c(n + 2);
  int min_bil = 0;
  RE (i, n) {
    cin>>c[i];
    if (c[i] == '(') {
      bil[i] = bil[i - 1] + 1;
    } else {
      bil[i] = bil[i - 1] - 1;
    }
    mini(min_bil, bil[i]);
  }
  FOR (i, 0, n) {
    bil[i] -= min_bil;
  }
  
  RE (i, n) {
    dc[i].Build(i);
  }


//#define TEST
#ifdef TEST
#ifdef LOCAL
  VVI ok(n + 2, VI(n + 2));
  RE (i, n) {
    int bbil = 0;
    FOR (j, i, n) {
      if (c[j] == ')') {
        bbil--;
      } else {
        bbil++;
      }
      if (bbil < 0) { break; }
      if (bbil == 0) { ok[i][j] = 1; }
    }
  }
  RE (i, n) {
    RE (j, n) {
      ok[i][j] += ok[i][j - 1];
    }
  }
  FORD (i, n, 1) {
    RE (j, n) {
      ok[i][j] += ok[i + 1][j];
    }
  }

  RE (j, n) {
    RE (i, j) {
      int q = Query(i, j);
      if (q != ok[i][j]) {
        debug(i, j, q, ok[i][j]);
        FOR (l, i, j) {
          cerr<<c[l];
        }
        cerr<<endl;
      }
      assert(q == ok[i][j]);
      //debug(i, j, Query(i, j), ok[i][j]);
    }
  }
  debug("succ");
#endif
#endif

  debug(Timestamp());

  
  auto Rem = [&](int i) {
    L[R[i]] = L[i];
    R[L[i]] = R[i];
    alive[i] = 0;
    //TODO wrzuc nowa pare
  };
  auto WhenSecondBetter = [&](int i, int j) {
    assert(i < j);
    int kl = j + 1, kp = n, faj = n + 1;
    while (kl <= kp) {
      int aktc = (kl + kp) / 2;
      if (Query(i + 1, aktc) + dp[i] < Query(j + 1, aktc) + dp[j]) {
        kl = aktc + 1;
      } else {
        faj = aktc;
        kp = aktc - 1;
      }
    }
    //debug(i, j, faj);
    return faj;
  };
  PII onL = {1, Query(1, n)};
  PII onR = {n, 0};
  int kl = 0, kp = n * n;
  while (kl <= kp) {
    int aktc = (kl + kp) / 2; // koszt ciachniecia
    dp[0] = 0;
    cnt_photos[0] = 0;
    RE (i, n + 1) {
      R[i - 1] = i;
      L[i] = i - 1;
      alive[i] = 1;
      evs[i].clear();
    }
    alive[0] = 1;
    R[n + 1] = 0;
    L[0] = n + 1;
    //debug(aktc);
    RE (i, n) {
      //debug(i, "in");
      for (int j = 0; j < SZ(evs[i]); j++) {
        PII p = evs[i][j];
        int a = p.st;
        int b = p.nd;
        if (!alive[a] || !alive[b]) { continue; }
        Rem(a);
        int z = L[b];
        if (z != n + 1) {
          int heh = WhenSecondBetter(z, b);
          evs[max(heh, i)].PB({z, b});
        }
      }
      int pred = R[n + 1];
      //debug(pred, i);
      dp[i] = dp[pred] + Query(pred + 1, i) + aktc;
      cnt_photos[i] = cnt_photos[pred] + 1;
      int z = L[i];
      if (z != n + 1) {
        int heh = WhenSecondBetter(z, i);
        evs[max(heh, i + 1)].PB({z, i});
      }
      //debug(i, pred, dp[i], cnt_photos[i]);
    }
    
    int real_val = dp[n] - cnt_photos[n] * aktc;
    //debug(cnt_photos[n], real_val);
    if (cnt_photos[n] == k) {
      assert(real_val % 2 == 0);
      cout<<real_val / 2<<"\n";
      return 0;
    } else if (cnt_photos[n] < k) {
      onL = {cnt_photos[n], real_val};
      kp = aktc - 1;
    } else {
      onR = {cnt_photos[n], real_val};
      kl = aktc + 1;
    }
  }
  int incl = (onR.nd - onL.nd) / (onR.st - onL.st);
  assert((onR.nd - onL.nd) % (onR.st - onL.st) == 0);
  int ans = onL.nd + incl * (k - onL.st);
  assert(ans % 2 == 0);
  cout<<ans / 2<<"\n";
    
    
    
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  return 0;
}