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;
  mt19937 rnd(2226701);
  const int INF = 0x3f3f3f3f;
  const int maxn = 55;
  int n, k;
  class Dsu {
   public:
    int fa[maxn];
    inline void init() {
      for (int i = 0; i < n; ++i) fa[i] = i;
      return;
    }
    inline int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); }
    inline bool merge(int x, int y) {
      x = find(x), y = find(y);
      if (x == y) return 0;
      fa[x] = y;
      return 1;
    }
  } dsu;
  int deg2[maxn], deg[maxn], d[maxn];
  int w[maxn][maxn], vis[maxn][maxn];
  inline bool cmp(const pair<int, int> &i, const pair<int, int> &j) {
    return w[i.first][i.second] < w[j.first][j.second];
  }
  inline int check() {
    vector<pair<int, int> > vec;
    dsu.init();
    int ret = 0, cnt = n;
    vector<pair<int, int> > fuck;
    for (int i = 0; i < n; ++i) {
      deg2[i] = deg[i];
      for (int j = i + 1; j < n; ++j) {
        if (vis[i][j]) {
          if (!dsu.merge(i, j)) return INF;
          ret += w[i][j];
          fuck.push_back(make_pair(i, j));
          --cnt;
        } else
          vec.push_back(make_pair(i, j));
      }
    }
    sort(vec.begin(), vec.end(), cmp);
    for (int i = 0; i < vec.size(); ++i)
      if (deg2[vec[i].first] < d[vec[i].first] &&
          deg2[vec[i].second] < d[vec[i].second] &&
          dsu.merge(vec[i].first, vec[i].second))
        ret += w[vec[i].first][vec[i].second], ++deg2[vec[i].first],
            ++deg2[vec[i].second], --cnt, fuck.push_back(vec[i]);
    if (cnt > 1) return INF;
    return ret;
  }
  int main() {
    scanf("%d%d", &n, &k);
    for (int i = 0; i < k; ++i) scanf("%d", d + i);
    for (int i = k; i < n; ++i) d[i] = INF;
    vector<pair<int, int> > all;
    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j < n; ++j) {
        scanf("%d", &w[i][j]);
        if (i < k) all.push_back(make_pair(i, j));
      }
    int ans = check(), cur = ans;
    for (long double T = 1000; T > 0.0001; T *= 0.9993) {
      int p = -1;
      for (p = rnd() % all.size(); (deg[all[p].first] == d[all[p].first] ||
                                    deg[all[p].second] == d[all[p].second]) &&
                                   !vis[all[p].first][all[p].second];
           p = rnd() % all.size())
        ;
      int u = all[p].first, v = all[p].second;
      deg[u] -= vis[u][v], deg[v] -= vis[u][v];
      vis[u][v] ^= 1;
      deg[u] += vis[u][v], deg[v] += vis[u][v];
      int delta = check() - cur;
      if (exp(-delta / T) >= (long double)(rnd() % 1000000000) / 1000000000) {
        cur += delta;
        ans = min(ans, cur);
      } else {
        deg[u] -= vis[u][v], deg[v] -= vis[u][v];
        vis[u][v] ^= 1;
        deg[u] += vis[u][v], deg[v] += vis[u][v];
      }
    }
    printf("%d\n", ans);
    return 0;
  }
  