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
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int N,K;
    string S;
    cin>>N>>K;
    if(N > 30) return 0;
    cin>>S;
    int DP[30][30];
    for(int i=0; i<N; ++i) for(int j=0; j<N; ++j) DP[i][j] = 0;
    for(int i=0; i<N; ++i) {
        for(int j=i; j<N; ++j) {
            for(int a=i; a<=j; ++a) for(int b=a; b<=j; ++b) {
                int L = 0, P = 0;
                bool cz = true;
                for(int l=a; l<=b; ++l) {
                    if(S[l] == '(') L++;
                    else P++;
                    if(P > L) cz = false;
                }
                if(P != L) cz = false;
                if(cz) DP[i][j]++;
            }
        }
    }
    int P = (1 << N-1) - 1;
    int MN = 1 << 30;
    for(int i=P; i>=0; --i) {
        int M = i, L = 0, Y = 1;
        int suma = 0;
        vector<int> V;
        V.push_back(0);
        while(M > 0) {
            if(M % 2 == 1) {
                L++;
                V.push_back(Y);
            }
            M /= 2;
            Y++;
        }
        V.push_back(N);
        if(L == K-1) {
            for(int i=0; i<V.size()-1; ++i) {
                suma += DP[V[i]][V[i+1]-1];
            }
            if(suma < MN) MN = suma;
            //cout<<suma<<endl;
        }
    }
    cout<<MN<<endl;
    
    
    return 0;
}