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
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

typedef struct {
	int end;
	int parent;
	int weight;
	vector<int> children;
} parexp_t;

typedef struct {
	int max;
	int max_pos;
	int total;
	vector<bool> starts;
	vector<parexp_t> exps;
} interval_t;

void parse_interval(interval_t& interval) {
	int n = interval.starts.size();
	parexp_t exp;
	exp.end = -1;
	exp.parent = -1;
	exp.weight = 0;
	interval.exps = vector<parexp_t>(n, exp);
	interval.max = 0;
	interval.max_pos = 0;
	interval.total = 0;
	stack<int> starts;
	for (int i = 0; i < n; ++i) {
		if (interval.starts[i]) {
			if (!starts.empty()) {
				interval.exps[starts.top()].children.push_back(i);
				interval.exps[i].parent = starts.top();
			}
			starts.push(i);
		}
		else if (!starts.empty()) {
			interval.exps[starts.top()].end = i;
			starts.pop();
		}
	}
	for (int i = 0; i < n; ++i) {
		if (interval.exps[i].parent != -1) {
			if (interval.exps[interval.exps[i].parent].end == -1) {
				interval.exps[i].parent = -1;
			}
		}
	}
	vector<int> parent_neighs;
	for (int i = 0; i < n; ++i) {
		if (interval.exps[i].end == -1) {
			continue;
		}
		if (interval.exps[i].parent != -1) {
			continue;
		}
		if (parent_neighs.size() > 0) {
			if (interval.exps[parent_neighs[parent_neighs.size() - 1]].end != i - 1) {
				for (int j = 0; j < parent_neighs.size(); ++j) {
					interval.exps[parent_neighs[j]].weight += (j + 1) * (parent_neighs.size() - j);
				}
				interval.total += parent_neighs.size() * (parent_neighs.size() - 1) / 2;
				parent_neighs.clear();
			}
		}
		parent_neighs.push_back(i);
	}
	for (int j = 0; j < parent_neighs.size(); ++j) {
		interval.exps[parent_neighs[j]].weight += (j + 1) * (parent_neighs.size() - j);
	}
	interval.total += parent_neighs.size() * (parent_neighs.size() - 1) / 2;
	for (int i = 0; i < n; ++i) {
		if (interval.exps[i].end == -1) {
			continue;
		}
		interval.exps[i].weight += interval.exps[i].parent == -1 ? 0 : interval.exps[interval.exps[i].parent].weight;
		int chsize = interval.exps[i].children.size();
		interval.total += 1 + chsize * (chsize - 1) / 2;
		for (int j = 0; j < interval.exps[i].children.size(); ++j) {
			int chld = interval.exps[i].children[j];
			interval.exps[chld].weight += (j + 1) * (chsize - j);
		}
	}
	for (int i = 0; i < n; ++i) {
		if (interval.exps[i].end == -1) {
			continue;
		}
		if (interval.exps[i].weight > interval.max) {
			interval.max = interval.exps[i].weight;
			interval.max_pos = i;
		}
	}
}

int main () {
	int n, k;
	scanf("%d %d\n", &n, &k);
	char c;
	interval_t interval;
	for (int i = 0; i < n; ++i) {
		scanf("%c", &c);
		interval.starts.push_back(c == '(');
	}
	parse_interval(interval);
	int total = interval.total;
	vector<interval_t> intervals;
	intervals.push_back(interval);
	priority_queue<pair<int, int> > q;
	if (interval.max) {
		q.push(make_pair(interval.max, 0));
	}
	for (int i = 0; i < k - 1; ++i) {
		if (q.empty()) {
			break;
		}
		pair<int, int> item = q.top();
		q.pop();
		int idx = item.second;
		total -= intervals[idx].max;
		int start = intervals[idx].max_pos;
		interval_t right;
		for (int j = start + 1; j < intervals[idx].starts.size(); ++j) {
			right.starts.push_back(intervals[idx].starts[j]);
		}
		parse_interval(right);
		intervals.push_back(right);
		intervals[idx].starts.resize(start + 1);
		parse_interval(intervals[idx]);
		if (intervals[idx].max) {
			q.push(make_pair(intervals[idx].max, idx));
		}
		if (intervals[intervals.size() - 1].max) {
			q.push(make_pair(intervals[intervals.size() - 1].max, intervals.size() - 1));
		}
	}
	printf("%d\n", total);
	return 0;
}