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
// Marcin Knapik

#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
using namespace std;

#define FOR(i, n) for (int i = 0; i < n; i++)
#define f first
#define s second
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

template <class T>ostream &operator<<(ostream &os, vector<T> &vec){for (T &el : vec){os << el << ' ';}return os;}
template <class T>istream &operator>>(istream &is, vector<T> &vec) {for (T &el : vec){is >> el;}return is;}

template <class T, class G> ostream &operator<<(ostream &os, pair<T, G> para) { os << para.f << ' ' << para.s; return os;}

using ll = long long;
using vi = vector<int>;
using ii = pair<int, int>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvpll = vector<vpll>;
using vii = vector<ii>;
using mp = map<vector<string>, long double>;

int n, k;
string s;

const int N = 4005;
int cost_tab[N][N];
int dp[N][N];

// int opt[]
const int INF = 1e9 + 7;

int cost(int pocz, int kon){
    assert(pocz <= kon);
    int ret = 0;

    vii stos;
    stos.pb({0, 1});

    int bil = 0;
    for(int i = pocz; i <= kon; i++){
        char u = s[i];
        if(u == '('){
            bil++;
            stos.pb({bil, 1});
        } else {
            bil--;
            stos.pop_back();
            if(sz(stos)){
                assert(stos.back().f == bil);
                ret += stos.back().s;
                stos.back().s++;
            }
            else {
                stos.pb({bil, 1});
            }
        }
    }

    return ret;
}

void compute_all_costs(){
    for(int start = 0; start < sz(s); start++){
        int ret = 0;
        int bil = 0;
        vii stos;
        stos.pb({0, 1});

        for(int kon = start; kon < sz(s); kon++){
            char u = s[kon];
            if(u == '('){
                bil++;
                stos.pb({bil, 1});
            } else {
                bil--;
                stos.pop_back();
                if(sz(stos)){
                    // cout << start << ' ' << kon << endl;
                    assert(stos.back().f == bil);
                    ret += stos.back().s;
                    stos.back().s++;
                }
                else {
                    stos.pb({bil, 1});
                }
            }
            cost_tab[start + 1][kon + 1] = ret;
        }
    }
}

void solve () {
    cin >> n >> k;
    cin >> s;
    
    if(n <= 18) {
        int ans = INF;
        for(int mask = 0; mask < (1 << (n - 1)); mask++){
            if(__builtin_popcount(mask) == k - 1){
                int last = 0;
                int score = 0;
                
                for(int j = 0; j < (n - 1); j++){
                    if(mask & (1 << j)){
                        score += cost(last, j);
                        last = j + 1;
                    }
                }
                score = score + cost(last, n - 1);

                ans = min(ans, score);
            }
        }
        cout << ans << '\n';
    } else if (n <= 300) {
        compute_all_costs();
        
        dp[0][0] = 0;
        for(int i = 1; i <= n; i++) {
            dp[0][i] = cost_tab[1][i];
        }

        for(int przedz = 1; przedz <= k - 1; przedz++){
            for(int i = 1; i <= n; i++){
                dp[przedz][i] = INF;
                if(i <= przedz - 1) {
                    dp[przedz][i] = 0; 
                } else {
                    for(int last = i - 1; last >= 1; last--){
                        dp[przedz][i] = min(dp[przedz][i], dp[przedz - 1][last] + cost_tab[last + 1][i]);
                    }
                }
            }
        }

        cout << dp[k - 1][n] << '\n';
    } else if (n <= 4000){
        compute_all_costs();

        dp[0][0] = 0;
        for(int i = 1; i <= n; i++) {
            dp[0][i] = cost_tab[1][i];
        }

        for(int przedz = 1; przedz <= k - 1; przedz++){
            function<void(int, int, int, int)> req = [&](int pocz, int kon, int pocz_opt, int kon_opt){
                if(kon < pocz){
                    return;
                }

                int mid = (pocz + kon) / 2;
                int opt = -1;
                dp[przedz][mid] = INF;

                for(int last = pocz_opt; last <= min(kon_opt, mid - 1); last++){
                    int val = dp[przedz - 1][last] + cost_tab[last + 1][mid];
                    if(val < dp[przedz][mid]){
                        dp[przedz][mid] = val;
                        opt = last;
                    }
                }
                req(pocz, mid - 1, pocz_opt, opt);
                req(mid + 1, kon, opt, kon_opt);
            };

            req(przedz, n, przedz - 1, n);
        }

        cout << dp[k - 1][n] << '\n';
    }
}

int main () {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int tests = 1;
    // cin >> tests;

    for (int test = 1; test <= tests; test++) {
        solve();
    }

    return 0;
}