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;
  const int N = 55, inf = 1e9;
  int n, k, u[N * N], v[N * N], w[N * N], s, t, d, in[N * N], c[N * N],
      q[N * N * N * N], dis[N * N], pr[N * N], a[N * N], lj[N][N], ans = inf, bg,
                                                                   ed;
  vector<int> e[N * N];
  struct d1 {
    int f[N];
    void init(int n) {
      for (int i = 1; i <= n; i++) f[i] = i;
    }
    int fa(int x) { return x == f[x] ? x : f[x] = fa(f[x]); }
    void mg(int u, int v) { f[fa(u)] = fa(v); }
    int ok(int u, int v) { return fa(u) != fa(v); }
  } t1, t2;
  struct d2 {
    int z[N], l[N];
    void init(int n) {
      for (int i = 1; i <= n; i++) z[i] = 0, l[i] = inf;
    }
    void mg(int u, int v) { z[u]++, z[v]++; }
    int ok(int u, int v) { return z[u] < l[u] && z[v] < l[v]; }
  } t3, t4, t5;
  int dfs() {
    for (int i = 1; i <= d + 2; i++) e[i].clear();
    t2 = t1, t5 = t4;
    for (int i = 1; i <= d; i++)
      if (a[i]) t2.mg(u[i], v[i]), t5.mg(u[i], v[i]);
    for (int i = 1; i <= d; i++) {
      if (!a[i]) {
        if (t2.ok(u[i], v[i])) e[s].push_back(i);
        if (t5.ok(u[i], v[i])) e[i].push_back(t);
      }
    }
    for (int i = 1; i <= d; i++) {
      if (a[i]) {
        t2 = t1, t5 = t4;
        for (int j = 1; j <= d; j++)
          if (i != j && a[j]) t2.mg(u[j], v[j]), t5.mg(u[j], v[j]);
        for (int j = 1; j <= d; j++) {
          if (!a[j]) {
            if (t2.ok(u[j], v[j])) e[i].push_back(j);
            if (t5.ok(u[j], v[j])) e[j].push_back(i);
          }
        }
      }
    }
    bg = ed = 0;
    for (int i = 1; i <= d + 2; i++) dis[i] = inf, pr[i] = c[i] = in[i] = 0;
    dis[s] = 0, q[ed++] = s, in[s] = 1;
    while (bg < ed) {
      int u = q[bg++];
      in[u] = 0;
      for (auto v : e[u]) {
        int nd = dis[u] + (a[v] ? -w[v] : w[v]);
        if (nd < dis[v] || (nd == dis[v] && c[u] + 1 < c[v])) {
          dis[v] = nd, c[v] = c[u] + 1, pr[v] = u;
          if (!in[v]) q[ed++] = v, in[v] = 1;
        }
      }
    }
    if (dis[t] == inf) return -1;
    for (int i = pr[t]; i != s; i = pr[i]) a[i] ^= 1;
    return dis[t];
  }
  int main() {
    scanf("%d%d", &n, &k);
    t3.init(n);
    for (int i = 1; i <= k; i++) scanf("%d", &t3.l[i]);
    for (int i = 1; i <= n; i++)
      for (int j = i + 1; j <= n; j++) scanf("%d", &lj[i][j]);
    for (int i = 1; i <= n; i++) {
      for (int j = max(i + 1, k + 1); j <= n; j++) {
        d++;
        u[d] = i, v[d] = j;
        w[d] = lj[i][j];
      }
    }
    s = d + 1, t = d + 2;
    int b = (1 << (k * (k - 1) / 2)) - 1;
    for (int x = 0; x <= b; x++) {
      t1.init(n);
      t4 = t3;
      for (int i = 1; i <= d; i++) a[i] = 0;
      int z = 0, f = 1, r = 0;
      for (int i = 1; i <= k; i++) {
        for (int j = i + 1; j <= k; j++) {
          if (x >> z & 1) {
            f &= t1.ok(i, j), f &= t4.ok(i, j);
            t1.mg(i, j), t4.mg(i, j);
            r += lj[i][j];
          }
          z++;
        }
      }
      if (!f) continue;
      int p = n - 1 - __builtin_popcount(x);
      while (p--) {
        int y = dfs();
        if (y == -1) {
          f = 0;
          break;
        }
        r += y;
      }
      if (f) ans = min(ans, r);
    }
    printf("%d\n", ans);
    return 0;
  }
  