#include <bits/stdc++.h> using namespace std; constexpr int maxN = 1e5+7; int n,k; long long ans=1e18; string s; bool check(vector<char>essa, int l, int r){ int ile = 0; for(int i=l;i<=r;i++){ if(essa[i] == ')') ile--; else ile++; if(ile < 0) return false; } return (ile == 0); } long long cnt(vector<char>essa){ long long res = 0; for(int i=0;i<essa.size();i++) for(int j=i;j<essa.size();j++) res += (int)check(essa,i,j); return res; } long long solve(int x){ long long res = 0; vector<char>essa; for(int i=0;i<n-1;i++){ essa.push_back(s[i]); if((1 << i) & x){ res += cnt(essa); essa.clear(); } } essa.push_back(s[n-1]); res += cnt(essa); return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>k; cin>>s; for(int i=0;i<(1 << (n-1));i++){ if(__builtin_popcount(i) != k-1) continue; ans = min(ans, solve(i)); } cout<<ans<<"\n"; } /* 15 2 ())(()())()(()) */
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 | #include <bits/stdc++.h> using namespace std; constexpr int maxN = 1e5+7; int n,k; long long ans=1e18; string s; bool check(vector<char>essa, int l, int r){ int ile = 0; for(int i=l;i<=r;i++){ if(essa[i] == ')') ile--; else ile++; if(ile < 0) return false; } return (ile == 0); } long long cnt(vector<char>essa){ long long res = 0; for(int i=0;i<essa.size();i++) for(int j=i;j<essa.size();j++) res += (int)check(essa,i,j); return res; } long long solve(int x){ long long res = 0; vector<char>essa; for(int i=0;i<n-1;i++){ essa.push_back(s[i]); if((1 << i) & x){ res += cnt(essa); essa.clear(); } } essa.push_back(s[n-1]); res += cnt(essa); return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>k; cin>>s; for(int i=0;i<(1 << (n-1));i++){ if(__builtin_popcount(i) != k-1) continue; ans = min(ans, solve(i)); } cout<<ans<<"\n"; } /* 15 2 ())(()())()(()) */ |