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
#include <bits/stdc++.h>


using namespace std;

using ll = long long int;
using ld = long double;

const int MAXN = 100010;


int n, k;
string s;


int h[MAXN];
int min_h = 0;
// previous position at the same height
int prev_h[MAXN];
// next position at the same height
int next_h[MAXN];

// number of left endpoints of bracket sequences ending at i
int left_endpoints[MAXN];
// number of right endpoints of bracket sequences starting at i
int right_endpoints[MAXN];
// smallest left endpoint of bracket sequence ending at i
int first_endpoint[MAXN];
// largest right endpoint of bracket sequence ending at i
int last_endpoint[MAXN];


void preprocessing() {
  map<int, int> last_h;
  last_h[0] = 0;
  prev_h[0] = -1;
  left_endpoints[0] = 0;
  first_endpoint[0] = 0;
  h[0] = 0;
  for (int i = 1; i <= n; i++) {
    h[i] = h[i - 1] + (s[i - 1] == '(' ? 1 : -1);
    min_h = min(min_h, h[i]);
    if (last_h.find(h[i]) == last_h.end()) {
      prev_h[i] = -1;
      left_endpoints[i] = 0;
      first_endpoint[i] = i;
    } else {
      prev_h[i] = last_h[h[i]];
      left_endpoints[i] = s[i - 1] == '(' ? 0 : left_endpoints[prev_h[i]] + 1;
      first_endpoint[i] = s[i - 1] == '(' ? i : first_endpoint[prev_h[i]];
    }
    last_h[h[i]] = i;
  }

  last_h.clear();
  last_h[h[n]] = n;
  next_h[n] = -1;
  right_endpoints[n] = 0;
  last_endpoint[n] = n;
  for (int i = n - 1; i >= 0; i--) {
    if (last_h.find(h[i]) == last_h.end()) {
      next_h[i] = -1;
      right_endpoints[i] = 0;
      last_endpoint[i] = i;
    } else {
      next_h[i] = last_h[h[i]];
      right_endpoints[i] = s[i] == ')' ? 0 : right_endpoints[next_h[i]] + 1;
      last_endpoint[i] = s[i] == ')' ? i : last_endpoint[next_h[i]];
    }
    last_h[h[i]] = i;
  }
}


ld score[MAXN];
int ncuts[MAXN];
// sequences cut at position i
ll new_cuts[MAXN];
// max score on [prev_h[i]; i) for i s.t. [prev_h[i]; i] is a valid bracket sequence
pair<ld, int> score_subseg_max[MAXN];

vector<tuple<ld, int, int>> hull[MAXN];
vector<ld> hull_intersections[MAXN];
// uuh
ld max_score_diff[MAXN];
int max_ncuts_diff[MAXN];
ll common_cuts_below[MAXN];


// assumes slope is >= than anything added
void hull_insert(int id, ld offset, int slope, int data) {
  if (hull[id].size() == 0) {
    hull[id].push_back({ offset, slope, data });
    hull_intersections[id].push_back(-1.0/0.0);
    return;
  }

  if (hull[id].size() == 1) {
    auto [offset_top, slope_top, data_top] = hull[id].back();
    if (slope == slope_top) {
      if (offset_top < offset) {
        hull[id].pop_back();
        hull[id].push_back({ offset, slope, data });
      }
      return;
    }
  }

  while (true) {
    auto [offset_top, slope_top, data_top] = hull[id].back();
    if (slope == slope_top) {
      if (offset_top < offset) {
        hull[id].pop_back();
        hull_intersections[id].pop_back();
      } else {
        return;
      }
    } else {
      ld new_intersection = (offset_top - offset) / (slope - slope_top);
      if (new_intersection < hull_intersections[id].back()) {
        hull[id].pop_back();
        hull_intersections[id].pop_back();
      } else {
        hull[id].push_back({ offset, slope, data });
        hull_intersections[id].push_back(new_intersection);
      }
    }
  }
}

pair<ld, int> hull_query(int id, int point) {
  auto it = upper_bound(hull_intersections[id].begin(), hull_intersections[id].end(), (ld)point);
  int p = it - hull_intersections[id].begin();

  auto [offset, slope, data] = hull[id][p - 1];
  return { offset + (ld)slope * point, data };
}


pair<ld, int> run_DP(ld penalty) {
  for (int i = 0; i <= n; i++) {
    score[i] = -1.0/0.0;
    ncuts[i] = 0;
    new_cuts[i] = 0;

    hull[i].clear();
    hull_intersections[i].clear();
    max_score_diff[i] = -1.0/0.0;
    max_ncuts_diff[i] = 0;
    common_cuts_below[i] = 0;
  }
  score[0] = 0.0;

  for (int i = 1; i <= n; i++) {
    // update cuts
    new_cuts[i] = new_cuts[i - 1];
    if (s[i - 1] == ')') {
      new_cuts[i] -= left_endpoints[i];

      // update hull
      hull[h[i] - min_h + 1].clear();
      hull_intersections[h[i] - min_h + 1].clear();
      if (prev_h[i] != -1) {
        int num_substrings = right_endpoints[first_endpoint[i]];
        hull_insert(
          h[i] - min_h,
          score[prev_h[i]] - ((ld)left_endpoints[i] - 1) * num_substrings,
          left_endpoints[i] - 1,
          ncuts[prev_h[i]]
        );

        if (i - 1 > prev_h[i]) {
          ld max_score = 0.0;
          int max_ncuts = 0;

          // this should amortize to O(n)
          score_subseg_max[i] = { score[i - 1], ncuts[i - 1] };
          if (prev_h[i] < i - 2) {
            int j = i - 1;
            while (prev_h[j] > prev_h[i]) {
              if (score_subseg_max[j].first > score_subseg_max[i].first) {
                score_subseg_max[i] = score_subseg_max[j];
              }
              if (score[prev_h[j]] > score_subseg_max[i].first) {
                score_subseg_max[i] = { score[prev_h[j]], ncuts[prev_h[j]] };
              }
              j = prev_h[j];
            }
          }

          hull_insert(
            h[i] - min_h,
            score_subseg_max[i].first - (ld)left_endpoints[i] * num_substrings,
            left_endpoints[i],
            score_subseg_max[i].second
          );
        }
      }
    } else {
      new_cuts[i] += right_endpoints[i - 1];
      common_cuts_below[h[i] - min_h] = new_cuts[i];
    }

    // calc score
    score[i] = new_cuts[i] - penalty;
    ncuts[i] = 1;
    if (i > 1) {
      if (s[i - 1] == '(') {
        ld new_score = score[i - 1] - new_cuts[i - 1] + new_cuts[i];
        if (new_score > score[i]) {
          score[i] = new_score;
          ncuts[i] = ncuts[i - 1];
        }
      } else {
        if (first_endpoint[i] > 1) {
          ld new_score = max_score_diff[first_endpoint[i] - 1] + new_cuts[i] - penalty;
          if (new_score > score[i]) {
            score[i] = new_score;
            ncuts[i] = max_ncuts_diff[first_endpoint[i] - 1] + 1;
          }
        }

        // best score from hull
        ld max_new_score = -1.0/0.0;
        int max_new_ncuts = 0;
        if (hull[h[i] - min_h].size() > 0) {
          tie(max_new_score, max_new_ncuts) = hull_query(h[i] - min_h, left_endpoints[i]);
        }
        max_new_score += new_cuts[i] - common_cuts_below[h[i] - min_h] - penalty;
        if (max_new_score > score[i]) {
          score[i] = max_new_score;
          ncuts[i] = max_new_ncuts + 1;
        }
      }
    }
    max_score_diff[i] = score[i] - new_cuts[i] + penalty;
    max_ncuts_diff[i] = ncuts[i] - 1;
  }

  ld best_score = 0;
  int best_ncuts = 0;
  for (int i = 0; i <= n; i++) {
    if (score[i] > best_score) {
      best_score = score[i];
      best_ncuts = ncuts[i];
    }
  }
  return { best_score, best_ncuts };
}


int main() {
  cin >> n >> k;
  k--;
  cin >> s;

  preprocessing();

  ll all_substrings = 0;
  for (int i = 1; i <= n; i++) {
    all_substrings += left_endpoints[i];
  }
  if (k == 0) {
    printf("%lld\n", all_substrings);
    return 0;
  }

  ld l = 0.0;
  ld r = all_substrings;
  ld score = 0.0;
  int ncuts = 0;
  for (int $ = 0; $ < 80; $++) {
    ld mid = (l + r) / 2;
    tie(score, ncuts) = run_DP(mid);
    if (ncuts == k) {
      ll true_score = (ll)round(score + k * mid);
      printf("%lld\n", all_substrings - true_score);
      return 0;
    } else if (ncuts < k) {
      r = mid;
    } else {
      l = mid;
    }
  }
  ll true_score = (ll)round(score + k * r);

  printf("%lld\n", all_substrings - true_score);
}