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
#include <iostream>
#include <vector>
#include <cmath>
#include <tuple>

using namespace std;

const int inf = 1 << 30;
const long long inf_ll = 1ll << 60;

struct li_chao {
    struct line {
        long long a, b, t;
        line(long long _a, long long _b, long long _t) : a(_a), b(_b), t(_t) {}
    };
    vector<line>l;
    int leaf_count;
    li_chao(int n) {
        for(leaf_count = 1; leaf_count < n; leaf_count *= 2) {}
        l.resize(leaf_count * 2, line(0, inf_ll, 0));
    }
    void insert(long long a, long long b, long long t) {
        insert_rec(a, b, t, 0, leaf_count, 1);
    }
    void insert_rec(long long a, long long b, long long t, int lo, int hi, int v) {
        int mid = (lo + hi) / 2;
        
        bool lef = a * lo + b < l[v].a * mid + l[v].b;
        bool mi = a * mid + b < l[v].a * mid + l[v].b;
        
        if(mi) {
            swap(a, l[v].a);
            swap(b, l[v].b);
            swap(t, l[v].t);
        }
        
        if(hi - lo == 1)
            return;
        
        if(lef != mi) {
            insert_rec(a, b, t, lo, mid, v * 2);
        } else {
            insert_rec(a, b, t, mid, hi, v * 2 + 1);
        }
    }
    pair<long long, int> get(long long x) {
        return get_rec(x, 0, leaf_count, 1);
    }
    pair<long long, int>get_rec(long long x, int lo, int hi, int v) {
        pair<long long, int>answer = make_pair(x * l[v].a + l[v].b, l[v].t);
        if(hi - lo == 1)
            return answer;
        int mid = (lo + hi) / 2;
        if(x < mid) {
            return min(get_rec(x, lo, mid, v * 2), answer);
        }
        return min(get_rec(x, mid, hi, v * 2 + 1), answer);
    }
};

struct seg_tree {
    vector<pair<int, int>>t;
    int leaf_count;
    seg_tree(vector<int>v) {
        int n = v.size();
        for(leaf_count = 1; leaf_count < n; leaf_count *= 2) {}
        t.resize(leaf_count * 2);
        for(int i = 0; i < n; i++)
            t[i + leaf_count] = make_pair(v[i], i);
        for(int i = n; i < leaf_count; i++)
            t[i + leaf_count] = make_pair(inf, i);
        for(int i = leaf_count - 1; i > 0; i--)
            t[i] = min(t[i * 2], t[i * 2 + 1]);
    }
    void set(int x, int value) {
        x += leaf_count;
        t[x].first = value;
        for(x /= 2; x > 0; x /= 2)
            t[x] = min(t[x * 2], t[x * 2 + 1]);
    }
    pair<int, int>get_min(int l, int r) {
        l += leaf_count;
        r += leaf_count;
        pair<int, int>answer = min(t[l], t[r]);
        while(l / 2 != r / 2) {
            if(l % 2 == 0)
                answer = min(answer, t[l + 1]);
            if(r % 2 == 1)
                answer = min(answer, t[r - 1]);
            l /= 2;
            r /= 2;
        }
        return answer;
    }
};


struct solver {
    struct node {
        vector<int>children;
        int l, r, min_count;
        long long answer;
        node() {}
        node(int _l, int _r, int _min_count, long long _answer) : l(_l), r(_r), min_count(_min_count), answer(_answer) {}
    };
    vector<node>nodes;
    
    solver(vector<int>&prefix_sum) {
        int n = (int)prefix_sum.size() - 1;
        seg_tree t(prefix_sum);
//         cout << "prefix_sum = ";
//         for(int x : prefix_sum)
//             cout << x << ' ';
//         cout << '\n';
        dfs_make(0, n, t);
    }
    
    int dfs_make(int l, int r, seg_tree &t) {
       // cout << "dfs_make at " << l << ' ' << r << '\n';
        int x = nodes.size();
        nodes.emplace_back(l, r, 0, 0);
        if(l >= r) {
            return x;
        }
        auto [my_lo, ind] = t.get_min(l, r);
        int next_lo, next_ind;
        vector<int>mins;
        do {
            mins.push_back(ind);
            t.set(ind, inf);
            tie(next_lo, next_ind) = t.get_min(l ,r);
            ind = next_ind;
        } while(next_lo == my_lo);
//         cout << "mins = ";
//         for(int x : mins)
//             cout << x << ' ';
//         cout << '\n';
        nodes[x].min_count = mins.size();
        nodes[x].answer = 1ll * nodes[x].min_count * (nodes[x].min_count - 1) / 2;
        int last = l;
        for(int k : mins) {
            int new_son = dfs_make(last, k - 1, t);
            nodes[x].children.push_back(new_son);
            nodes[x].answer += nodes[new_son].answer;
            last = k + 1;
        }
        int new_son = dfs_make(last, r, t);
        nodes[x].children.push_back(new_son);
        nodes[x].answer += nodes[new_son].answer;
     //   cout << l << ' ' << r << ", answer = " << nodes[x].answer << '\n';
        return x;
    }
    
    pair<long long, int> get_answer(long long C) {
        auto [answer, taken] = dp_dfs(0, C);
        if(nodes[0].answer < answer)
            return make_pair(nodes[0].answer, 1);
        return make_pair(answer, taken + 1);
    }
    
    pair<long long, int> dp_dfs(int x, long long C) { //how many 
        
        if(nodes[x].children.empty())
            return make_pair(C, 1);
        
        vector<pair<long long, int>>children_dp;
        for(int y : nodes[x].children)
            children_dp.push_back(dp_dfs(y, C));
        
        vector<long long>dp(nodes[x].children.size(), inf_ll);
        vector<int>taken(nodes[x].children.size(), 1);
        
        vector<long long>answer_pref(1, 0);
        for(int y : nodes[x].children)
            answer_pref.push_back(answer_pref.back() + nodes[y].answer);
        
        li_chao lt(nodes[x].children.size());
        
        for(int i = 0; i < (int)nodes[x].children.size(); i++) {
            dp[i] = answer_pref[i] + 1ll * i * (i - 1) / 2 + children_dp[i].first;
            taken[i] = children_dp[i].second;
            
            auto [left_best, left_taken] = lt.get(i);
            left_best += answer_pref[i] + i * (i - 1) / 2 + children_dp[i].first;
            
            if(left_best < dp[i]) {
                dp[i] = left_best;
                taken[i] = left_taken + children_dp[i].second;
            }
//             for(int j = 0; j < i; j++) {
//                 
//                 long long new_value = answer_pref[i] - answer_pref[j + 1] + dp[j] + children_dp[i].first;
//                 long long len = i - j;
//                 new_value += len * (len - 1) / 2;
//                 
//                 if(new_value < dp[i]) {
//                     dp[i] = new_value;
//                     taken[i] = taken[j] + children_dp[i].second;
//                 }
//             }
            
            lt.insert(-i, i * (i + 1) / 2 + dp[i] - answer_pref[i + 1], taken[i]);
        }
//         cout << "dp = ";
//         for(long long t : dp)
//             cout << t << ' ';
//         cout << '\n';
//         cout << "taken = ";
//         for(int t : taken)
//             cout << t << ' ';
//         cout << '\n';
//         
        long long my_answer = inf_ll;
        int my_taken = 0;
        
        for(int i = 0; i < (int)nodes[x].children.size(); i++) {
            
            long long right = (int)nodes[x].children.size() - i - 1;
            long long my_value = dp[i] + right * (right - 1) / 2 + answer_pref.back() - answer_pref[i + 1];
            
            if(my_value < my_answer) {
                my_answer = my_value;
                my_taken = taken[i];
            }
        }
        
    //    cout << "answer at " << nodes[x].l << ' ' << nodes[x].r << ": " << my_answer << ' ' << my_taken << '\n';
        
        return make_pair(my_answer, my_taken);
    }
};

int main() {
    ios_base::sync_with_stdio(0);
    int n, k; cin >> n >> k;
    string s; cin >> s; s = '#' + s;
    
    vector<int>prefix_sum(n + 1);
    prefix_sum[0] = n;
    for(int i = 1; i <= n; i++)
        prefix_sum[i] = prefix_sum[i - 1] + (s[i] == ')' ? -1 : 1);
    
//     cout << "prefix_sum = ";
//     for(int x : prefix_sum)
//         cout << x << ' ';
//     cout << '\n';
    
    solver S(prefix_sum);
    
   // long long lo = 0, hi = 1ll * n * n;
    long long lo = 0, hi = 1ll * n * n;
    while(lo < hi) {
        long long mid = (lo + hi) / 2;
    //    cout << "MID = " << mid << '\n';
        auto [result, taken] = S.get_answer(mid);
      //  cout << "result = " << result << ", taken = " << taken << '\n';
        if(taken <= k) {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }
    
   // cout << "hi = " << hi << '\n';

    
    cout << S.get_answer(hi).first - (k - 1) * hi << '\n';
    
    return 0;
}