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
#include <bits/stdc++.h>
#define ll long long
#define debug if(0)

const int MAX_N = 1e5;
int t[MAX_N+3];
std::vector<int> pos[MAX_N+3];
ll n, k, m;
ll MAX_RES;

void input() {
    std::cin >> n >> k;
    std::string s; std::cin >> s;

    m = 0;
    int S = 0;

    std::set<int> set_of_sums; set_of_sums.insert(0);
    std::map<int, int> sum_to_color; sum_to_color[0] = 0;
    pos[0].push_back(0);
    for (int i = 0; i < s.length(); i++) {
        if (s[i] == '(') S++;
        else S--;
        while (!set_of_sums.empty() && (*set_of_sums.rbegin()) > S) 
            set_of_sums.erase(*set_of_sums.rbegin());
        if (!set_of_sums.count(S)) { 
            set_of_sums.insert(S);
            sum_to_color[S] = ++m;
        }
        t[i+1] = sum_to_color[S];
        pos[t[i+1]].push_back(i+1);
    }

    MAX_RES = 0;
    for (int i = 0; i <= m; i++) {
        ll color_cnt = pos[i].size();
        MAX_RES += ((color_cnt-1)*color_cnt) / 2;
    }

    debug {
        std::cout << "for string\n" << s << "\n";
        std::cout << "colors are:\n";
        for (int i = 0; i <= n; i++) std::cout << t[i] << " ";
        std::cout << "\n";
        std::cout << "MAX_RES = " << MAX_RES << "\n";
    }
}

int find_first(int l, int r, int x) {
    int b = 0; int e = pos[x].size()-1; int mid;
    while (b < e) {
        int mid = (b+e)/2;
        if (pos[x][mid] >= l) e = mid;
        else b = mid+1;
    }
    if (pos[x][b] >= l && pos[x][b] <= r) return b;
    return n+1;
}

int find_last(int l, int r, int x) {
    int b = 0; int e = pos[x].size()-1; int mid;
    while (b < e) {
        int mid = (b+e+1)/2;
        if (pos[x][mid] <= r) b = mid;
        else e = mid-1;
    }
    if (pos[x][b] >= l && pos[x][b] <= r) return b;
    return -1;
}

ll count_pos(int l, int r, int x) {
    int first = find_first(l, r, x);
    int last = find_last(l, r, x);

    debug std::cout << "count_pos(" << l << ", " << r << ", " << x << ") = [" << first << ", " << last << "]\n";

    if (first <= last) return last-first+1;
    return 0;
}

std::pair<ll, int> dp[MAX_N+3];
ll f[MAX_N+3];

inline ll get_val(int j) {
    return dp[j].first + f[j];
}

std::pair<ll, int> calc_dp(ll C) {
    debug std::cout << "trying C = " << C << "\n";

    dp[0] = {0, 0}; f[0] = 0;
    std::vector<int> opt_prev = {0};
    std::vector<int> temp;
    for (int i = 1; i <= n; i++) {
        debug {
            std::cout << "opt_prev before " << i << ":\n";
            for (auto j : opt_prev) 
                std::cout << j << ": " << dp[j].first << " + " << f[j] << "\n";
        }

        for (auto j : opt_prev) 
            f[j] += count_pos(j, i, t[i]) - 1;
        temp.clear(); temp = {opt_prev[0]};
        for (int j = 1; j < opt_prev.size(); j++) 
            if (get_val(opt_prev[j]) < get_val(temp.back())) 
                temp.push_back(opt_prev[j]);
        dp[i].first = get_val(temp.back()) + C;
        dp[i].second = dp[temp.back()].second + 1;
        f[i] = 0;
        opt_prev.clear(); opt_prev = {i};
        for (auto j : temp)
            if (get_val(j) < get_val(opt_prev.back()))
                opt_prev.push_back(j);
    }

    debug std::cout << "returning dp[" << n << "] = {" << dp[n].first << ", " << dp[n].second << "}\n";
    return dp[n];
}

ll psearch() {
    ll b = 0;
    ll e = MAX_RES+3;
    ll mid;
    std::pair<ll, ll> p;
    while (b < e) {
        debug std::cout << "BINSEARCH RANGE: [" << b << ", " << e << "]\n";

        mid = (b+e+1)/2;
        p = calc_dp(mid);
        if (p.second == k) { return p.first - mid * p.second; }
        else if (p.second > k) b = mid;
        else e = mid-1; 
    }
    p = calc_dp(b);
    ll sol = p.first - b * p.second;
    sol += (p.second - k) * b;
    return sol;
}

int main() {
    std::ios_base::sync_with_stdio(0); std::cin.tie(NULL);
    input();
    std::cout << psearch() << "\n";
}