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
// while (clock()<=69*CLOCKS_PER_SEC)
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("O3")
// #pragma GCC target ("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                    \
    sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(c i) \
    {
sim > struct rge {
    c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef XOX
    ~debug()
    {
        cerr << endl;
    }
    eni(!=) cerr << boolalpha << i;
    ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d)
{
    ris << "" << d.first << " --> " << d.second << "";
}
sim dor(rge<c> d)
{
    *this << "[";
    for(auto it = d.b; it != d.e; ++it)
        *this << ", " + 2 * (it == d.b) << *it;
    ris << "]";
}
#else
    sim dor(const c &)
    {
        ris;
    }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#ifdef XOX
#warning Times may differ!!!
#endif

#define endl '\n'
#define pb emplace_back
#define vt vector
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int nax = 1e6, mod = 1e9 + 7;

#define int long long

// https://robert1003.github.io/2020/02/29/dp-opt-knuth.html
struct Node {
    int p, l, r;
};

struct Tree {
    typedef int T;
    static constexpr T unit = INT_MAX;
    T f(T a, T b) { return min(a, b); } // (any associative fn)
    vector<T> s;
    int n;
    Tree(int n = 0, T def = unit) : s(2 * n, def), n(n) {}
    void update(int pos, T val)
    {
        for(s[pos += n] = val; pos /= 2;)
            s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
    }
    T query(int b, int e)
    { // query [b, e)
        T ra = unit, rb = unit;
        for(b += n, e += n; b < e; b /= 2, e /= 2) {
            if(b % 2) ra = f(ra, s[b++]);
            if(e % 2) rb = f(s[--e], rb);
        }
        return f(ra, rb);
    }
};

int32_t main()
{
    cin.tie(0)->sync_with_stdio(0);
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    s = "#" + s;
    vt<int> to(n + 2, -1);
    // can do
    vt<int> st;
    for(int i = 1; i <= n; i++) {
        if(s[i] == ')') {
            if(sz(st)) {
                to[st.back()] = i + 1;
                st.pop_back();
            }
        }
        else {
            st.pb(i);
        }
    }
    debug() << imie(to);
    int p = 1;
    vt<int> chain_id(n + 2);
    function<int(int)> dfs = [&](int u) {
        chain_id[u] = p;
        int c = 0;
        if(to[u] != -1) {
            c += dfs(to[u]) + 1;
        }
        return c;
    };
    for(int i = 1; i <= n; i++) {
        if(!chain_id[i]) {
            dfs(i);
            p++;
        }
    }
    // process costs, TODO: speed this up
    vt<vt<ll>> cost(n + 1, vt<ll>(n + 2));
    auto C = [&](int x) {
        return (ll)x * (x + 1) / 2;
    };
    for(int i = 1; i <= n; i++) {
        vt<int> have(n + 1, -1);
        int ans = 0;
        for(int j = i; j <= n + 1; j++) {
            ans -= C(have[chain_id[j]]);
            have[chain_id[j]]++;
            ans += C(have[chain_id[j]]);
            cost[i][j] = ans;
        }
    }
    const int oo = 1e18 + 7;
    auto calc = [&](int delta) {
        vt<int> dp(n + 1, oo);
        vt<int> cnt(n + 1);
        dp[0] = 0;
        auto f = [&](int i, int j) {
            return dp[i] + cost[i + 1][j + 1] + delta;
        };
        deque<Node> dq;
        dq.push_back({0, 1, n});
        for(int i = 1; i <= n; i++) {
            dp[i] = f(dq.front().p, i);
            cnt[i] = cnt[dq.front().p] + 1;
            if(dq.front().r == i)
                dq.pop_front();
            else
                dq.front().l++;
            while(!dq.empty() && f(i, dq.back().l) < f(dq.back().p, dq.back().l))
                dq.pop_back();
            if(dq.empty())
                dq.push_back({i, i + 1, n});
            else {
                int l = dq.back().l, r = dq.back().r;
                while(l < r) {
                    int mid = r - (r - l) / 2;
                    if(f(i, mid) < f(dq.back().p, mid))
                        r = mid - 1;
                    else
                        l = mid;
                }
                dq.back().r = l;
                if(l != n) dq.push_back({i, l + 1, n});
            }
        }
        return pair<int, int>{dp[n], cnt[n]};
    };
    int lo = 0, hi = (int)n * n, ans = -1;
    while(lo <= hi) {
        int mid = (lo + hi) / 2;
        pair<int, int> res = calc(mid);
        if(res.second <= k) {
            ans = res.first - k * mid;
            hi = mid - 1;
        }
        else {
            lo = mid + 1;
        }
    }
    cout << ans << endl;
}