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;
  int n, k, d[55], G[55][55];
  int bas[55][55], fa[55], las[55], fucked[55][55];
  int ans = 1e9;
  int GF(int x) { return x == fa[x] ? x : fa[x] = GF(fa[x]); }
  namespace nz {
  vector<int> G[3030];
  int st = 3001, en = 3002, val[3030];
  void init() {
    for (int i = 0; i <= en; ++i) G[i].clear();
  }
  void adde(int x, int y) { G[x].push_back(y); }
  int d[3030], in[3030], pre[3030];
  int fuck() {
    memset(d, 33, sizeof d);
    memset(in, 0, sizeof in);
    memset(pre, 0, sizeof pre);
    d[st] = 0;
    queue<int> q;
    q.push(st);
    for (int tim = 0; q.size();) {
      int x = q.front();
      q.pop();
      in[x] = 0;
      for (auto y : G[x]) {
        int z = d[x] + val[y];
        if (z < d[y]) {
          d[y] = z;
          pre[y] = x;
          if (!in[y]) q.push(y), in[y] = 1;
        }
      }
    }
    int sum = 0;
    for (int p = en; p; p = pre[p]) {
      if (p != st && p != en) {
        int i = (p - 1) / n, j = (p - 1) % n + 1;
        bas[i][j] ^= 1;
      }
      sum += val[p];
    }
    return d[en];
  }
  }  // namespace nz
  int ID(int i, int j) { return i * n + j; }
  int ID(pair<int, int> p) { return ID(p.first, p.second); }
  void supermain() {
    for (int i = 1; i <= k; ++i) fa[i] = i, las[i] = d[i];
    int nowsum = 0, aim = n - 1;
    for (int i = 1; i <= k; ++i)
      for (int j = i + 1; j <= k; ++j)
        if (bas[i][j]) {
          nowsum += G[i][j];
          if (GF(i) == GF(j)) return;
          fa[GF(i)] = GF(j);
          --las[i];
          --las[j];
          --aim;
          if (las[i] < 0 || las[j] < 0) return;
        }
    for (; aim--;) {
      nz::init();
      for (int i = 1; i <= n; ++i) fa[i] = i, las[i] = d[i];
      vector<pair<int, int> > vec, vecall;
      for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
          if (bas[i][j]) {
            if (i <= k) --las[i];
            if (j <= k) --las[j];
            fa[GF(i)] = GF(j);
            vecall.push_back({i, j});
            if (j > k) vec.push_back({i, j});
          }
      for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j)
          nz::val[ID(i, j)] = bas[i][j] ? -G[i][j] : G[i][j];
      memset(fucked, 0, sizeof fucked);
      for (int i = 1; i <= n; ++i)
        for (int j = i + 1; j <= n; ++j) {
          if (j <= k) continue;
          if (bas[i][j]) continue;
          if (GF(i) != GF(j)) nz::adde(nz::st, ID(i, j)), fucked[i][j] = 1;
          if (i > k || las[i] > 0) nz::adde(ID(i, j), nz::en);
        }
      for (auto o : vec) {
        for (int i = 1; i <= n; ++i) fa[i] = i;
        for (auto p : vecall)
          if (p != o) fa[GF(p.first)] = GF(p.second);
        for (int i = 1; i <= n; ++i)
          for (int j = i + 1; j <= n; ++j) {
            if (j <= k) continue;
            if (bas[i][j]) continue;
            if (GF(i) != GF(j) && !fucked[i][j]) nz::adde(ID(o), ID(i, j));
            if (i <= k && las[i] == 0 && i == o.first) nz::adde(ID(i, j), ID(o));
          }
      }
      nowsum += nz::fuck();
      if (nowsum > 1e8) return;
    }
    ans = min(ans, nowsum);
  }
  int main() {
    cin >> n >> k;
    for (int i = 1; i <= k; ++i) cin >> d[i];
    for (int i = 1; i <= n; ++i) {
      for (int j = i + 1; j <= n; ++j) {
        cin >> G[i][j];
        G[j][i] = G[i][j];
      }
    }
    for (int S = 0; S < (1 << k * (k - 1) / 2); ++S) {
      memset(bas, 0, sizeof bas);
      int cnt = 0;
      for (int i = 1; i <= k; ++i)
        for (int j = i + 1; j <= k; ++j) {
          int c = S >> cnt & 1;
          cnt += 1;
          bas[i][j] = bas[j][i] = c;
        }
      supermain();
    }
    cout << ans << endl;
  }
  