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
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <queue>
#include <bitset>
#include <utility>
#include <stack>

using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define MP make_pair
#define FOR(v,p,k) for(int v=(p);v<=(k);++v)
#define FORD(v,p,k) for(int v=(p);v>=(k);--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()

#define ODD(x) ((x)%2)
#define EVEN(x) (!(ODD(x)))

char t[500009];

int main() {
    int n, k;
    scanf("%d %d\n", &n, &k);
    vector<int> t(n);
    REP(i, n) {
        char c;
        scanf("%c", &c);
        t[i] = (c == '(') ? 1 : -1;
    }
    vector<vector<LL>> naw(n, vector<LL>(n));
    VI stack;
    vector<LL> stack_res_acc;
    stack.reserve(n);
    stack_res_acc.reserve(n);
    REP(i, n) {
        stack.clear();
        stack.PB(0);
        stack_res_acc.clear();
        stack_res_acc.PB(0);
        LL res = 0;
        FOR(j, i, n-1) {
            if (t[j] > 0) {
                if (stack.empty()) {
                    stack.PB(0);
                    stack_res_acc.PB(0);
                }
                stack.PB(0);

                LL new_res = (stack_res_acc.size() > 0) ? stack_res_acc.back() : 0LL;
                stack_res_acc.PB(new_res);
            } else {
                if (stack.size() > 0) {
                    int num_at_level = stack.back();
                    res += (LL) (num_at_level+1) * num_at_level / 2;
                    stack.pop_back();
                    stack_res_acc.pop_back();
                    if (stack.size() > 0) {
                        stack.back() += 1;

                        LL res_single_top_level = (LL) (stack.back() + 1) * stack.back() / 2;
                        LL res_prev_levels_acc = (stack_res_acc.size() >= 2) ? stack_res_acc[stack_res_acc.size()-2] : 0LL;
                        stack_res_acc.back() = res_prev_levels_acc + res_single_top_level;
                    } 
                }
            }
            /**
            printf("stack [%d %d]: ", i, j);
            FOREACH(it, stack) {
                printf("%d ", *it);
            }
            printf("\n");
            */

            LL res_from_lower_levels = (stack_res_acc.size() > 0) ? stack_res_acc.back() : 0LL;
            naw[i][j] = res + res_from_lower_levels;
            /**
            LL new_res = naw[i][j];
            naw[i][j] = res; //old
            FOREACH(it, stack) {
                naw[i][j] += (LL) (*it + 1) * (*it) / 2;
            }
            assert (new_res == naw[i][j]);
            */
       }
    }

    /**
    REP(i, n) REP(j,n) {
        printf("t [%d %d]: ", i, j);
        FOR(c, i, j) {
            printf("%c", t[c] > 0 ? '(' : ')');
        }
        printf("\n");
        printf("naw [%d %d]: %lld\n", i, j, naw[i][j]);
    }
    */
    constexpr LL MX = (LL) 100009 * 100009;
    vector<vector<LL>> dp(k+1, vector<LL>(n, MX));
    REP(i, n) dp[1][i] = naw[i][n-1];

    FOR(kk, 2, k) {
        FORD(i, n-2, 0) {
            LL best = MX;
            FOR(j, i+1, n-1) {
                LL curr = naw[i][j-1] + dp[kk-1][j];
                best = min(best, curr);

                //printf("dp[%d][%d] cand=%lld\n", kk, i, curr);
            }
            dp[kk][i] = best;
            //printf("dp[%d][%d] best=%lld\n", kk, i, dp[kk][i]);
        }
    }
	
    printf("%lld\n", dp[k][0]);
    return 0;
}