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<bits/stdc++.h>
using namespace std;
#ifdef LOC
template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; }
template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) {
    out << "{";
    bool fi = 1;
    for(auto b : a) {
        if(fi) {out<<b; fi=0;}
        else out<<", "<<b;
    }
    return out << "}";
}

template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; }
void dump(){
   cerr << "\e[39m"<<"\n";
}
template <class T, class... Ts> void dump(T a, Ts... x) {
   cerr << a << ", ";
   dump(x...);
}
#define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#define debug(...) ;
#endif

#define fwd(i, a, b) for(int i=(a);i<(int)(b);i++)
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define all(X) (X).begin(), (X).end()
#define sz(X) ((int)(X).size())
#define mp make_pair
#define st first
#define nd second

typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;


vector<string> balancedSplit(string s) {
    int prev = 0;
    vector<string> res;
    int bal = 0;
    rep(i,sz(s)) {
        if(s[i] =='(') bal++;
        else bal--;
        if(bal < 0) {
            if(i > prev)
                res.push_back(s.substr(prev,i-prev));
            prev = i+1;
            bal = 0;
        };
    }
    string lastPiece = s.substr(prev);
    if(bal > 0) {
        for(auto &a: lastPiece)
            a = (a == '(' ? ')' : '(');
        reverse(all(lastPiece));
        auto res2 = balancedSplit(lastPiece);
        reverse(all(res2));
        for(auto &v:res2) {
            reverse(all(v));
            for(auto &a:v)
                a = (a == '(' ? ')' : '(');
        }
        res.insert(res.end(), all(res2));
    }
    else if(lastPiece != "")
        res.push_back(lastPiece);
    return res;
}


struct S {
    ll dp;
    ll dp0;
    int k;
    S(ll _dp, ll _dp0, int _k) : dp(_dp), dp0(_dp0), k(_k){}
};


typedef pair<ll,ll> P;
bool det(const P&a, const P&b, const P&c) {
    return (a.st-c.st)*(__int128_t)(b.nd-c.nd) >= (a.nd-c.nd)*(__int128_t)(b.st-c.st);
}

struct ChtMax {
    deque<P> hull;
    void add(P a) {
        while(hull.size() >= 2 && det(hull.back(),a,hull[hull.size()-2]))
            hull.pop_back();
        hull.push_back(a);
    }

    pll query(ll x) {
        while(sz(hull) >= 2 && hull[0].st*x+hull[0].nd <= hull[1].st*x+hull[1].nd)
            hull.pop_front();
        return {hull[0].st*x+hull[0].nd, hull[0].st};
    }
};

pll par2(string s, ll penalty) {
    vector<vector<int> > levels(sz(s)+1);
    int bal = 0;
    levels[0].push_back(0);
    rep(i,sz(s)) {
        if(s[i] == '(')
            bal++;
        else bal--;
        assert(bal >= 0);
        levels[bal].push_back(i+1);
    }
    assert(bal == 0);

    deque<S> prz;
    deque<S> prz2;

    auto joinAll = [&](vector<int> tab) {
        tab.pop_back();

        vector<pll> v0, v1;
        for(auto _:tab) {
            auto s = prz.front();
            prz.pop_front(); 
            v1.push_back({s.dp, s.k});
            v0.push_back({s.dp0, 0});
        }
        vl ps0 = {v0[0].st};
        fwd(i,1,sz(v0))
            ps0.push_back(v0[i].st + ps0[i-1]);

        vector<pll> dpAny(sz(tab), pll(1e18, -1));
        vector<pll> dp1(sz(tab), pll(1e18, -1));
        dp1[0] = v1[0];
        if(sz(tab) != 1)
            dpAny[0] = {v0[0].st+1, 0};
        dpAny[0] = min(dpAny[0], dp1[0]);
        ll lastPreDpAny = ps0[0] + 1;


        ChtMax cht;
        cht.add({0,ps0[0]-dp1[0].st});
        fwd(i,1,sz(tab)) {
            dp1[i] = {dpAny[i-1].st + v1[i].st, dpAny[i-1].nd + v1[i].nd};
            lastPreDpAny = ps0[i] + (i+2)*(ll)(i+1)/2;
            if(i+1 != sz(tab)) //force to use sth
                dpAny[i] = {lastPreDpAny, 0};

            auto res = cht.query(i);
            dpAny[i] = min(dpAny[i], {ps0[i]+i*(ll)(i+1)/2 - res.st, dp1[res.nd].nd});
            dpAny[i] = min(dpAny[i], dp1[i]);
            cht.add({i, ps0[i] - dp1[i].st - i*(ll)(i-1)/2});
        }
        assert(dpAny.back().nd > 0);
        prz2.push_back(S(dpAny.back().st, lastPreDpAny, dpAny.back().nd));
    };

    for(int z=sz(levels)-1;z>=0;z--) {
        if(levels[z].empty()) continue;
        vector<int> tab;
        for(auto i: levels[z]) {
            tab.push_back(i);
            if(i == sz(s) || s[i] == ')') {
                if(sz(tab) == 1)
                    prz2.push_back(S(penalty, 0, 1));
                else
                    joinAll(tab);
                tab.clear();
            }
        }
        if(sz(tab) == 1)
            prz.push_back(S(penalty, 0, 1));
        else if(sz(tab) > 1)
            joinAll(tab);
        tab.clear();
        assert(sz(prz) == 0);
        swap(prz, prz2);
    }
    assert(sz(prz) == 1);
    auto cur = *prz.begin();
    return min(pll(cur.dp0, 0), pll(cur.dp, cur.k));
}

int solve(string s, int k) {
    k--;
    ll low = 0, high = 1e12, mid ,lastGood=-1, lastAbove=-1;
    pair<ll,ll> lastVal, lastValAbove;
    auto splitted = balancedSplit(s);
    int totalSize = 0;
    for(auto&a : splitted) totalSize += sz(a);
    if(totalSize <= k) return 0;
    while(low <= high ){
        mid = (low + high)/2;
        
        pll res = {0,0};
        for(const auto& a:splitted) {
            auto v = par2(a, mid);
            res.st += v.st;
            res.nd += v.nd;
        }
        if(res.nd <= k) {
            lastGood = mid;
            lastVal = res;
            high = mid-1;
        }
        else {
            lastAbove = mid;
            lastValAbove = res;
            debug(mid, res);
            low = mid+1;
        }
    }
    if(lastVal.nd == k)
        return lastVal.st-k*lastGood;
    if(lastGood == 0)
        return 0;
    assert(lastAbove != -1);
    return max(lastValAbove.st-k*lastAbove, lastVal.st-k*lastGood);
}


int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,k;
    cin >> n >> k;
    string s;
    cin >> s;
    cout<<solve(s,k)<<"\n";
}