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>
  class disjoint_set {
   public:
   protected:
    std::vector<std::size_t> fa;
  
   public:
    disjoint_set(std::size_t n = 0) : fa(n) {
      std::iota(fa.begin(), fa.end(), 0);
    }
    std::size_t find(std::size_t x) {
      return fa[x] == x ? x : (fa[x] = find(fa[x]));
    }
    bool merge(std::size_t x, std::size_t y) {
      x = find(x), y = find(y);
      if (x == y) {
        return false;
      }
      fa[y] = x;
      return true;
    }
  };
  std::mt19937 rnd(std::chrono::system_clock().now().time_since_epoch().count());
  int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(0);
    int n, k;
    std::cin >> n >> k;
    std::vector<int> lim(n, n);
    for (int i = 0; i < k; ++i) {
      std::cin >> lim[i];
    }
    std::vector<std::vector<int>> w(n, std::vector<int>(n));
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        std::cin >> w[i][j];
        w[j][i] = w[i][j];
      }
    }
    int sum = 0;
    std::vector<std::pair<int, int>> edge;
    int rt = rnd() % (n - k) + k;
    for (int i = 0; i < n; ++i) {
      if (rt != i) {
        edge.emplace_back(rt, i);
        sum += w[rt][i];
      }
    }
    int ans = sum;
    for (double T = 1000000; T >= 1e-5; T *= 0.99996) {
      auto tmp(edge);
      tmp.erase(tmp.begin() + rnd() % (n - 1));
      disjoint_set D(n);
      std::vector<int> deg(n);
      int now = 0;
      for (auto [u, v] : tmp) {
        D.merge(u, v);
        ++deg[u], ++deg[v];
        now += w[u][v];
      }
      int x, y;
      do {
        x = rnd() % n, y = rnd() % n;
      } while (D.find(x) == D.find(y) || deg[x] == lim[x] || deg[y] == lim[y]);
      tmp.emplace_back(x, y);
      now += w[x][y];
      if (now < sum || exp((sum - now) / T) >= 1.0 * rnd() / rnd.max()) {
        ans = std::min(ans, now);
        edge.swap(tmp);
        sum = now;
      }
    }
    std::cout << ans << "\n";
  }
  