description: <image>
  
  William really likes puzzle kits. For one of his birthdays, his friends gifted him a complete undirected edge-weighted graph consisting of n vertices.
  
  He wants to build a spanning tree of this graph, such that for the first k vertices the following condition is satisfied: the degree of a vertex with index i does not exceed d_i. Vertices from k + 1 to n may have any degree.
  
  William wants you to find the minimum weight of a spanning tree that satisfies all the conditions.
  
  A spanning tree is a subset of edges of a graph that forms a tree on all n vertices of the graph. The weight of a spanning tree is defined as the sum of weights of all the edges included in a spanning tree.
  
  Input
  
  The first line of input contains two integers n, k (2 ≤ n ≤ 50, 1 ≤ k ≤ min(n - 1, 5)).
  
  The second line contains k integers d_1, d_2, …, d_k (1 ≤ d_i ≤ n). 
  
  The i-th of the next n - 1 lines contains n - i integers w_{i,i+1}, w_{i,i+2}, …, w_{i,n} (1 ≤ w_{i,j} ≤ 100): weights of edges (i,i+1),(i,i+2),…,(i,n).
  
  Output
  
  Print one integer: the minimum weight of a spanning tree under given degree constraints for the first k vertices. 
  
  Example
  
  Input
  
  
  10 5
  5 3 4 2 1
  29 49 33 12 55 15 32 62 37
  61 26 15 58 15 22 8 58
  37 16 9 39 20 14 58
  10 15 40 3 19 55
  53 13 37 44 52
  23 59 58 4
  69 80 29
  89 28
  48
  
  
  Output
  
  
  95
solution: #include <bits/stdc++.h>
  using namespace std;
  using LL = long long;
  constexpr int maxn = 50;
  int w[maxn][maxn];
  int p[maxn];
  int gp(int u) { return p[u] == u ? u : p[u] = gp(p[u]); }
  vector<pair<int, int>> pr[100 + 1];
  int n, k, d[maxn], e[maxn];
  pair<int, vector<pair<int, int>>> get(vector<pair<int, int>>& cur, int u,
                                        int v) {
    for (int i = 0; i < n; i += 1) p[i] = i;
    vector<pair<int, int>> ncur;
    LL nans = 0;
    for (int i = 0; i < k; i += 1) e[i] = 0;
    for (auto [x, y] : cur) {
      if (w[u][v] < w[x][y]) {
        int pu = gp(u), pv = gp(v);
        if (pu != pv) {
          p[pu] = pv;
          ncur.push_back({u, v});
          nans += w[u][v];
          e[u] += 1;
          e[v] += 1;
        }
      }
      int px = gp(x), py = gp(y);
      if (px != py) {
        p[px] = py;
        ncur.push_back({x, y});
        nans += w[x][y];
        e[x] += 1;
        e[y] += 1;
      }
    }
    for (int i = 0; i < k; i += 1)
      if (e[i] > d[i]) return {INT_MAX, {}};
    return {nans, ncur};
  }
  int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n >> k;
    for (int i = 0; i < k; i += 1) cin >> d[i];
    for (int i = 0; i < n; i += 1)
      for (int j = i + 1; j < n; j += 1) {
        cin >> w[i][j];
        w[j][i] = w[i][j];
        pr[w[i][j]].push_back({i, j});
      }
    for (int i = k; i < n; i += 1) p[i] = i;
    vector<pair<int, int>> p;
    for (int i = 0; i < n; i += 1)
      for (int j = i + 1; j < n; j += 1) p.push_back({i, j});
    int res = INT_MAX;
    srand(time(0));
    for (int i = 0; i < 500; i += 1) {
      random_shuffle(p.begin(), p.end());
      int ans = 0;
      vector<pair<int, int>> cur;
      int r = rand() % (n - k) + k;
      for (int i = 0; i < n; i += 1)
        if (i != r) {
          cur.push_back({i, r});
          ans += w[i][r];
        }
      sort(cur.begin(), cur.end(),
           [&](const pair<int, int>& p, const pair<int, int>& q) {
             return w[p.first][p.second] < w[q.first][q.second];
           });
      while (true) {
        res = min(res, ans);
        int ok = 1;
        for (auto [i, j] : p) {
          auto [nans, ncur] = get(cur, i, j);
          if (nans < ans) {
            ans = nans;
            ncur.swap(cur);
            ok = 0;
          }
        }
        if (ok) break;
      }
      res = min(res, ans);
    }
    cout << res;
    return 0;
  }
  