#include <bits/stdc++.h>
using namespace std;
int n, k;
string s;
int cnt1, cnt2;
int dp[20][20];
bool ok;
int solve(int begin, int nr){
if(!nr)
return 0;
if(nr == 1)
return dp[begin][n-1];
int res2 = 0, res = 1e9+7;
for(int i = begin; i < n-nr+1; i++){
res2 = dp[begin][i] + solve(i+1, nr-1);
res = min(res2, res);
}
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> s;
if(n > 18){
cout << "SEX";
return 0;
}
for(int i = 0; i < n; i++){
for(int j = i; j < n; j++){
ok = 1;
cnt1 = 0;
cnt2 = 0;
for(int x = i; x <= j; x++){
if(s[x] == '(')
cnt1++;
else
cnt2++;
if(cnt2 > cnt1)
ok = 0;
}
if(ok && cnt1 == cnt2){
for(int ii = 0; ii <= i; ii++){
for(int jj = j; jj < n; jj++){
dp[ii][jj]++;
}
}
}
}
}
// for(int i = 0; i < n; i++){
// for(int j = i; j < n; j++){
// cout << dp[i][j] << ' ';
// }
// cout << '\n';
// }
// cout << dp[0][4] << ' ' << dp[5][12] << ' ' << dp[13][14] << '\n';
// cout << "LUBIEWDUPE\n";
cout << solve(0, k);
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 | #include <bits/stdc++.h> using namespace std; int n, k; string s; int cnt1, cnt2; int dp[20][20]; bool ok; int solve(int begin, int nr){ if(!nr) return 0; if(nr == 1) return dp[begin][n-1]; int res2 = 0, res = 1e9+7; for(int i = begin; i < n-nr+1; i++){ res2 = dp[begin][i] + solve(i+1, nr-1); res = min(res2, res); } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k >> s; if(n > 18){ cout << "SEX"; return 0; } for(int i = 0; i < n; i++){ for(int j = i; j < n; j++){ ok = 1; cnt1 = 0; cnt2 = 0; for(int x = i; x <= j; x++){ if(s[x] == '(') cnt1++; else cnt2++; if(cnt2 > cnt1) ok = 0; } if(ok && cnt1 == cnt2){ for(int ii = 0; ii <= i; ii++){ for(int jj = j; jj < n; jj++){ dp[ii][jj]++; } } } } } // for(int i = 0; i < n; i++){ // for(int j = i; j < n; j++){ // cout << dp[i][j] << ' '; // } // cout << '\n'; // } // cout << dp[0][4] << ' ' << dp[5][12] << ' ' << dp[13][14] << '\n'; // cout << "LUBIEWDUPE\n"; cout << solve(0, k); return 0; } |
polski