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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <iostream>
#include <string>


using namespace std;
//() )   (() ())()(())  ()()             ((| () ()) (() ()))  6, , (((| ) ()) (() ()))       () (() ()  (((| )

int Max(int n, int *value)
{
    int max = 0;
    int zwroc = 0;
    for (int i = 0; i < n; i++)
    {
        if (value[i] > max)
        {
            max = value[i];
            value[i] = 0;
            zwroc = i;
        }
    }
    return zwroc;
}

int NAW(int l, int r, string tab)
{
    bool byl=0;
    int amount = 0;
    int otwartyNawias = 0;
    int zaliczonyWyraz = 0;
    if (tab[l] == '(')
    {
        otwartyNawias++;
    }
    l++;
    //cout << "hello" << endl;
    //cout << "r+1 to: " << r+1<<endl;
    while (l != r+1)
    {
        if (otwartyNawias < 0)
            otwartyNawias = 0;

        if (zaliczonyWyraz < 0)
            zaliczonyWyraz = 0;

        /* if ((tab[l - 1] == '(' && tab[l] == ')') && ((l) != (r)))
         {
             amount++;
             if (counter>0)
             {
                 amount++;
                 counter--;
             }
             counter++;



             cout << "1 to  else if l to: " << l << endl<< " zmienna byl to: " << byl << endl << " amount to: " << amount << endl << " a counter to: " << counter << endl;
         }
         else if ((tab[l - 1] == '(' && tab[l] == '(') && ((l) != (r)))
         {
             counter++;

             cout << "2 else if l to: " << l << endl << " zmienna byl to: " << byl << endl << " amount to: " << amount << endl << " a counter to: " << counter << endl;
         }
         else if ((tab[l - 1] == ')' && tab[l] != '(') && (counter>0) && ((l) != (r)))
         {
             byl = 0;
             cout << "3 else to l to: " << l << endl << " zmienna byl to: " << byl << endl << " amount to: " << amount << endl << " a counter to: " << counter << endl;
             counter--;
         }
         else if ((tab[l - 1] == ')' && tab[l] == ')') && ((l) != (r)))
             counter--;


         l++;
     }*/

      if (tab[l - 1] == '(' && tab[l] == '(')
        {
            otwartyNawias++;
            //cout << "pierwszy if, otwartych nawiasow to: " << otwartyNawias << " amount to: " << amount << " zaliczonyWyraz " << zaliczonyWyraz << " L wynosi: " << l << endl;
        }
        else if (tab[l - 1] == '(' && tab[l] == ')')
        {
            amount++;
            otwartyNawias--;
            if (otwartyNawias > 0)
            {
                amount++;
            }
            zaliczonyWyraz++;
            //cout << "drugi if, otwartych nawiasow to: " << otwartyNawias << " amount to: " << amount << " zaliczonyWyraz " << zaliczonyWyraz << " L wynosi: " << l << endl;
        }
        else if (tab[l - 1] == ')' && tab[l] == ')' && otwartyNawias > 0)
        {
            amount++;
            otwartyNawias--;
            zaliczonyWyraz--;
           // cout << "trzeci if, otwartych nawiasow to: " << otwartyNawias << " amount to: " << amount << " zaliczonyWyraz " << zaliczonyWyraz << " L wynosi: " << l << endl;
        }
        else if (tab[l - 1] == ')' && tab[l] == '(')
        {
            /*amount++;

            if (zaliczonyWyraz > 0)
            {
                amount++;
                zaliczonyWyraz--;
            }
                otwartyNawias++;*/
            if (otwartyNawias > 0)
            {
                zaliczonyWyraz++;
                amount++;
            }
            otwartyNawias++;

           // cout << "czwarty if, otwartych nawiasow to: " << otwartyNawias << " amount to: " << amount << " zaliczonyWyraz " << zaliczonyWyraz << " L wynosi: " << l << endl;
        }
        else
        {
            zaliczonyWyraz = 0;
            otwartyNawias = 0;
        }
       //cout <<endl <<endl << "poza ifem otwartych nawiasow to: " << otwartyNawias << " amount to: " << amount << " zaliczonyWyraz " << zaliczonyWyraz << " L wynosi: " << l << endl;
       //cout << "tu skonczylem"<<endl;
       l++;
      // cout << "r+1 to: " << r + 1 << endl;
      // cout << "a l to: " << l << endl;
    }



    return amount;
}
int main()
{
    int n, k;

    char otw = '(';
    char zam = ')';

    int temp1 = 0;
    int sum=0;

    cin >> n;
    cin >> k;

    string tab;
    
    int* pomoc = new int[n];

    cin >> tab;

    int* pom = new int[k+1];
    pom[0] = 0;
    for (int i = 1; i < k; i++)
        pom[i] = 0;

    pom[k] = n-1;

    for(int i = 0; i<n; i++)
    {
        pomoc[i] = 0;
    }

    for (int i = 0; i < n; i++)
    {
        if (tab[i] == otw)
        {
            temp1++;
        }
        else if(tab[i] == zam && i>0 && temp1 != 0)
        {
            pomoc[i-1] = temp1;
            temp1 = 0;
        }
    }
    
    //for (int i = 0; i < k + 1; i++)
    //{
    //    cout << "przedprzed i to:" << i << "wwartosc to: " << pom[i] << endl;
    //}

    for (int i = 1; i < k; i++)
    {
        pom[i] = Max(n, pomoc);
    }
    
    //for (int i = 0; i < k + 1; i++)
    //{
    //    cout << "przed i to:" << i << "wwartosc to: " << pom[i] << endl;
    //}

    for (int i = 0; i < k; i++)
    {
        for (int j = 0; j < k; j++)
        {
            if (pom[j] > pom[j + 1])
                swap(pom[j], pom[j + 1]);
        }
    }
    int i = 0;
//    tak wyglada tablica
    for (i = 0; i<k; i++)
    {
     //   cout << "i to:" <<i<< "wwartosc to: "<< pom[i] << endl;
        if (pom[i + 1] > 0)
        {
            pom[i] = -1;
            break;
        }
    }

    bool lec = 1;

    for (i = 0; i < k+1; i++)
    {
        if (pom[i] == 0)
        {
            lec = 0;
        }
    }

   /* for (int i = 0; i < k + 1; i++)
    {
        cout << "po i to:" << i << "wwartosc to: " << pom[i] << endl;
    }*/

    if (lec)
    {
        for (i=0; i < k; i++)
        {
            sum += NAW(pom[i] + 1, pom[i + 1], tab);
        }
    }

    cout << sum;
    

}