#include <bits/stdc++.h>
using namespace std;
using T = unsigned;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k; cin >> n >> k;
if (n < 3) {
cout << 0 << endl;
return 0;
}
string word; cin >> word;
vector<int> maxPos;
unordered_map<int, int> maxes;
maxes[0] = 0;
maxPos.push_back(0);
for (int i = 0, cnt = 1; i < n; ++i) {
if (i == n - 1) {
maxes[n] = cnt++;
maxPos.push_back(n);
continue;
}
if (word[i] == '(' and word [i + 1] == ')') {
maxes[i + 1] = cnt++;
maxPos.push_back(i + 1);
}
}
if (k >= size(maxPos) - 1) {
cout << 0 << endl;
return 0;
}
vector<vector<T>> h(size(maxes));
for (auto &l : h) l.resize(size(maxes));
for (auto startPos : maxPos) {
stack<int> s;
s.push(1);
T acc{};
for (int i = startPos; i < n; ++i) {
if (i > 0 and word[i - 1] == '(' and word[i] == ')') h[maxes[startPos]][maxes[i]] = acc;
if (word[i] == '(') {
s.push(1);
} else {
s.pop();
if (s.empty()) s.push(0);
acc += s.top();
++s.top();
}
}
h[maxes[startPos]][maxes[n]] = acc;
}
vector<vector<pair<T, int>>> dp(k);
for (auto &dpp : dp) dpp.resize(size(maxPos), {0, 1});
for (int nn = 1; nn < size(dp[0]); ++nn) {
for (int kk = 1; kk <= size(dp); ++kk) {
if (kk == 1) {
dp[0][nn] = {h[0][nn], 1};
continue;
}
T acc = numeric_limits<T>::max();
int best = kk - 1;
for (int tmp = dp[kk - 2][nn - 1].second; tmp < nn; ++tmp) {
T x = dp[kk - 2][tmp].first + h[tmp][nn];
if (x <= acc) {
acc = x;
best = tmp;
}
}
dp[kk - 1][nn] = {acc, best};
}
}
cout << dp.back().back().first << endl;
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; using T = unsigned; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; if (n < 3) { cout << 0 << endl; return 0; } string word; cin >> word; vector<int> maxPos; unordered_map<int, int> maxes; maxes[0] = 0; maxPos.push_back(0); for (int i = 0, cnt = 1; i < n; ++i) { if (i == n - 1) { maxes[n] = cnt++; maxPos.push_back(n); continue; } if (word[i] == '(' and word [i + 1] == ')') { maxes[i + 1] = cnt++; maxPos.push_back(i + 1); } } if (k >= size(maxPos) - 1) { cout << 0 << endl; return 0; } vector<vector<T>> h(size(maxes)); for (auto &l : h) l.resize(size(maxes)); for (auto startPos : maxPos) { stack<int> s; s.push(1); T acc{}; for (int i = startPos; i < n; ++i) { if (i > 0 and word[i - 1] == '(' and word[i] == ')') h[maxes[startPos]][maxes[i]] = acc; if (word[i] == '(') { s.push(1); } else { s.pop(); if (s.empty()) s.push(0); acc += s.top(); ++s.top(); } } h[maxes[startPos]][maxes[n]] = acc; } vector<vector<pair<T, int>>> dp(k); for (auto &dpp : dp) dpp.resize(size(maxPos), {0, 1}); for (int nn = 1; nn < size(dp[0]); ++nn) { for (int kk = 1; kk <= size(dp); ++kk) { if (kk == 1) { dp[0][nn] = {h[0][nn], 1}; continue; } T acc = numeric_limits<T>::max(); int best = kk - 1; for (int tmp = dp[kk - 2][nn - 1].second; tmp < nn; ++tmp) { T x = dp[kk - 2][tmp].first + h[tmp][nn]; if (x <= acc) { acc = x; best = tmp; } } dp[kk - 1][nn] = {acc, best}; } } cout << dp.back().back().first << endl; return 0; } |
English