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[6], a[55][55], ans = 1000000007, res, f[55], cnt;
  vector<pair<int, int> > v[105];
  int get(int x) { return x == f[x] ? x : f[x] = get(f[x]); }
  void sol() {
    vector<int> V;
    for (int i = 1; i <= n; ++i) V.push_back(f[i]);
    int R = res, C = cnt;
    vector<int> q;
    for (int i = 1; i <= 100; ++i) {
      for (int j = 0; j < v[i].size(); ++j) {
        int x = v[i][j].first, y = v[i][j].second;
        if (x <= k || y <= k || get(x) == get(y)) continue;
        f[f[y]] = f[x];
        res += a[x][y];
      }
      for (int j = 0; j < v[i].size(); ++j) {
        int x = v[i][j].first, y = v[i][j].second;
        if ((x > k && y > k) || get(x) == get(y)) continue;
        if (x <= k) {
          if (!d[x]) continue;
          --d[x];
          q.push_back(x);
          ++cnt;
        }
        if (y <= k) {
          if (!d[y]) continue;
          --d[y];
          q.push_back(y);
          ++cnt;
        }
        f[f[y]] = f[x];
        res += a[x][y];
      }
    }
    for (int i = 0; i < q.size(); ++i) ++d[q[i]];
    q.clear();
    if (cnt == n - 1) ans = min(ans, res);
    for (int _ = 0; _ < 100; ++_) {
      for (int i = 1; i <= n; ++i) f[i] = V[i - 1];
      res = R;
      cnt = C;
      for (int i = 1; i <= 100; ++i) {
        random_shuffle(v[i].begin(), v[i].end());
        for (int j = 0; j < v[i].size(); ++j) {
          int x = v[i][j].first, y = v[i][j].second;
          if (x <= k || y <= k || get(x) == get(y)) continue;
          f[f[y]] = f[x];
          res += a[x][y];
          ++cnt;
        }
        for (int j = 0; j < v[i].size(); ++j) {
          int x = v[i][j].first, y = v[i][j].second;
          if ((x > k && y > k) || get(x) == get(y)) continue;
          if (rand() < 2000) continue;
          if (x <= k) {
            if (!d[x]) continue;
            --d[x];
            q.push_back(x);
          }
          if (y <= k) {
            if (!d[y]) continue;
            --d[y];
            q.push_back(y);
          }
          f[f[y]] = f[x];
          res += a[x][y];
          ++cnt;
        }
      }
      if (cnt == n - 1) ans = min(ans, res);
      for (int i = 0; i < q.size(); ++i) ++d[q[i]];
      q.clear();
    }
    for (int _ = 0; _ < 100; ++_) {
      for (int i = 1; i <= n; ++i) f[i] = V[i - 1];
      res = R;
      cnt = C;
      for (int i = 1; i <= 100; ++i) {
        random_shuffle(v[i].begin(), v[i].end());
        for (int j = 0; j < v[i].size(); ++j) {
          int x = v[i][j].first, y = v[i][j].second;
          if (x <= k || y <= k || get(x) == get(y)) continue;
          f[f[y]] = f[x];
          res += a[x][y];
          ++cnt;
        }
        for (int j = 0; j < v[i].size(); ++j) {
          int x = v[i][j].first, y = v[i][j].second;
          if ((x > k && y > k) || get(x) == get(y)) continue;
          if (rand() < 6000) continue;
          if (x <= k) {
            if (!d[x]) continue;
            --d[x];
            q.push_back(x);
          }
          if (y <= k) {
            if (!d[y]) continue;
            --d[y];
            q.push_back(y);
          }
          f[f[y]] = f[x];
          res += a[x][y];
          ++cnt;
        }
      }
      if (cnt == n - 1) ans = min(ans, res);
      for (int i = 0; i < q.size(); ++i) ++d[q[i]];
      q.clear();
    }
    for (int _ = 0; _ < 100; ++_) {
      for (int i = 1; i <= n; ++i) f[i] = V[i - 1];
      res = R;
      cnt = C;
      for (int i = 1; i <= 100; ++i) {
        random_shuffle(v[i].begin(), v[i].end());
        for (int j = 0; j < v[i].size(); ++j) {
          int x = v[i][j].first, y = v[i][j].second;
          if (x <= k || y <= k || get(x) == get(y)) continue;
          f[f[y]] = f[x];
          res += a[x][y];
          ++cnt;
        }
        for (int j = 0; j < v[i].size(); ++j) {
          int x = v[i][j].first, y = v[i][j].second;
          if ((x > k && y > k) || get(x) == get(y)) continue;
          if (rand() < 16000) continue;
          if (x <= k) {
            if (!d[x]) continue;
            --d[x];
            q.push_back(x);
          }
          if (y <= k) {
            if (!d[y]) continue;
            --d[y];
            q.push_back(y);
          }
          f[f[y]] = f[x];
          res += a[x][y];
          ++cnt;
        }
      }
      if (cnt == n - 1) ans = min(ans, res);
      for (int i = 0; i < q.size(); ++i) ++d[q[i]];
      q.clear();
    }
    for (int i = 1; i <= n; ++i) f[i] = V[i - 1];
    res = R;
    cnt = C;
  }
  void dfs(int x, int y) {
    if (x > k) {
      sol();
      return;
    }
    if (y > k) {
      dfs(x + 1, x + 2);
      return;
    }
    if (d[x] && d[y] && get(x) != get(y)) {
      vector<int> V;
      for (int i = 1; i <= n; ++i) V.push_back(f[i]);
      f[f[y]] = f[x];
      --d[x], --d[y];
      res += a[x][y];
      ++cnt;
      dfs(x, y + 1);
      --cnt;
      res -= a[x][y];
      ++d[x], ++d[y];
      for (int i = 1; i <= n; ++i) f[i] = V[i - 1];
    }
    dfs(x, y + 1);
  }
  int main() {
    srand(time(0));
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= k; ++i) scanf("%d", d + i);
    for (int i = 1; i <= n; ++i) f[i] = i;
    for (int i = 1; i <= n; ++i) {
      for (int j = i + 1; j <= n; ++j) {
        scanf("%d", a[i] + j);
        a[j][i] = a[i][j];
        if (i > k || j > k) v[a[i][j]].push_back(make_pair(i, j));
      }
    }
    dfs(1, 2);
    printf("%d\n", ans);
    return 0;
  }
  