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
// NAW
#ifndef __SIZEOF_INT128__
#define __SIZEOF_INT128__
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
template <typename T> using oset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define rep(i, p, k) for(int i(p); i < k; ++i)
#define per(i, p, k) for(int i(p); i > k; --i)
#define sz(x) (int)x.size()
#define sc static_cast
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef __int128_t lll;
//#define int ll
template <typename T = int> using par = std::pair <T, T>;
#define fir first
#define sec second
#define test int _number_of_tests(in()); while(_number_of_tests--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb emplace_back
struct Timer {
    string name{""};
    time_point<high_resolution_clock> end, start{high_resolution_clock::now()};
    duration<float, std::milli> dur;
    Timer() = default;
    Timer(string name): name(name) {}
    ~Timer() {
        end = high_resolution_clock::now(); dur= end - start;
        cout << "@" << name << "> " << dur.count() << " ms" << '\n';
    }
};
template <typename T = int> inline T in()
{
    T x;
    std::cin >> x;
    return x;
}
std::string yn(bool b)
{
    if(b) return "YES\n";
    else return "NO\n";
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par);
template <typename T> std::ostream& operator<< (std::ostream& out, const std::vector <T>& wek)
{
    for(const auto& i : wek)out << i << ' ';
    return out;
}
template <typename F, typename S> std::ostream& operator<<(std::ostream& out, const std::pair <F, S>& par)
{
    out << '{'<<par.first<<", "<<par.second<<"}";
    return out;
}
#define show(x) cout << #x << " = " << x << '\n';
constexpr int MAXN = 1e5 + 3;
bool tab[MAXN];
map <int, int> wyst;
vector <int> syn[MAXN];
int ojc[MAXN];
int uniqind(){ static int i(0); return ++i;}
vector <ll> dp0[MAXN];
void licz(int w, int k)
{
    for(auto i: syn[w])licz(i, k+1);
    dp0[w].resize(k+1);
    const int s(syn[w].size());
    vector <ll> pre(s+1);
    rep(i, 1, s+1)pre[i] = pre[i-1] + dp0[syn[w][i-1]][0];
    vector <vector <ll>> dp1(s+1, vector <ll>(k+1));
    rep(i, 1, s+1)rep(j, 0, k+1){
        dp1[i][j] = 1e18;
        rep(l, 0, i)rep(d, 1, j+1)dp1[i][j] = min(dp1[i][j], dp1[l][j-d] + dp0[syn[w][i-1]][d] + ll(i-l-1)*(i-l)/2 + pre[i-1] - pre[l]);
    }
    rep(j, 0, k+1){
        dp0[w][j] = 1e18;
        rep(i, 0, s+1)dp0[w][j] = min(dp0[w][j], dp1[i][j] + pre[s] - pre[i] + ll(s-i+1)*(s-i)/2);
    }
}
std::int32_t main()
{
    int n, k, w(0);
    scanf("%d %d ", &n, &k);
    vector <pair <int, int>> roty;
    wyst[0] = -1;
    rep(i, 0, n){
        tab[i] = getchar_unlocked() == ')';
        w += tab[i] ? -1 : +1;
        if(wyst.count(w)){
            while(sz(roty) && roty.back().first > wyst[w])roty.pop_back();
            roty.pb(wyst[w]+1, i);
        }
        else wyst[w] = i;
        while(wyst.upper_bound(w) != wyst.end())wyst.erase(wyst.upper_bound(w));
    }
    for(auto [p, k]: roty){
        int g(uniqind());
        syn[0].pb(g);
        rep(i, p, k+1){
            if(tab[i])g = ojc[g];
            else{
                syn[g].pb(uniqind());
                ojc[syn[g].back()] = g;
                g = syn[g].back();
            }
        }
    }
    for(auto i: syn[0])licz(i, k-1);
    const int s(sz(syn[0]));
    vector <vector <ll>> dp2(s+1, vector <ll>(k));
    rep(i, 1, s+1)rep(j, 0, k){
        dp2[i][j] = 1e18;
        rep(d, 0, j+1)dp2[i][j] = min(dp2[i][j], dp2[i-1][j-d] + dp0[syn[0][i-1]][d]);
    }
    cout << dp2[s][k-1] << '\n';
    return 0;
}