#include <bits/stdc++.h>
using namespace std;
const int N = 53, mod = 1e9 + 7;
int n, k;
int d[N], a[N][N];
vector<pair<int, int> > tmp;
set<pair<int, int> > edge;
int siz[N];
vector<int> g[N];
int f[N];
int fa(int x) { return f[x] == x ? x : f[x] = fa(f[x]); }
int dif;
pair<int, int> del, ad;
int f2[N];
void dfs(int x, int fat) {
  f2[x] = fat;
  for (int v : g[x])
    if (v != fat) {
      dfs(v, x);
    }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; ++i) scanf("%d", d + 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];
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j) tmp.push_back(make_pair(i, j));
  int ans = mod;
  for (int tim = 1; tim <= 100; ++tim) {
    int now = 0;
    for (int i = 1; i <= n; ++i) f[i] = i;
    edge.clear();
    for (int i = 1; i <= n; ++i) siz[i] = 0;
    random_shuffle(begin(tmp), end(tmp));
    while ((int)edge.size() < n - 1) {
      for (auto& p : tmp) {
        if (p.first <= k && siz[p.first] == d[p.first]) continue;
        if (p.second <= k && siz[p.second] == d[p.second]) continue;
        if (p.first <= k && p.second <= k && siz[p.first] == d[p.first] - 1 &&
            siz[p.second] == d[p.second] - 1)
          continue;
        int x = fa(p.first), y = fa(p.second);
        if (x == y) continue;
        f[x] = y;
        now += a[p.first][p.second];
        ++siz[p.first];
        ++siz[p.second];
        edge.insert(p);
      }
    }
    a[0][0] = -mod;
    int cnt0 = 0;
    for (;;) {
      for (int i = 1; i <= n; ++i) g[i].clear();
      for (auto& p : edge)
        g[p.first].push_back(p.second), g[p.second].push_back(p.first);
      for (int i = 1; i <= n; ++i) siz[i] = g[i].size();
      dif = mod;
      for (int i = 1; i <= n; ++i) {
        dfs(i, 0);
        for (int j = 1; j <= n; ++j) {
          int kk = j;
          while (kk != i) {
            bool flag = 0;
            --siz[kk];
            --siz[f2[kk]];
            ++siz[i];
            ++siz[j];
            for (int p = 1; p <= k; ++p)
              if (siz[p] > d[p]) {
                flag = 1;
                break;
              }
            ++siz[kk];
            ++siz[f2[kk]];
            --siz[i];
            --siz[j];
            if (!flag && dif >= a[i][j] - a[kk][f2[kk]]) {
              dif = a[i][j] - a[kk][f2[kk]];
              del = make_pair(min(kk, f2[kk]), max(kk, f2[kk]));
              ad = make_pair(min(i, j), max(i, j));
            }
            kk = f2[kk];
          }
        }
      }
      if (dif <= 0) {
        if (dif == 0)
          ++cnt0;
        else
          cnt0 = 0;
        if (cnt0 == 10) break;
        now += dif;
        edge.erase(del);
        edge.insert(ad);
      } else
        break;
    }
    ans = min(ans, now);
  }
  printf("%d\n", ans);
  return 0;
}

### Prompt

Your challenge is to write a CPP solution to the following problem:

<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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 53, mod = 1e9 + 7;
int n, k;
int d[N], a[N][N];
vector<pair<int, int> > tmp;
set<pair<int, int> > edge;
int siz[N];
vector<int> g[N];
int f[N];
int fa(int x) { return f[x] == x ? x : f[x] = fa(f[x]); }
int dif;
pair<int, int> del, ad;
int f2[N];
void dfs(int x, int fat) {
  f2[x] = fat;
  for (int v : g[x])
    if (v != fat) {
      dfs(v, x);
    }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; ++i) scanf("%d", d + 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];
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j) tmp.push_back(make_pair(i, j));
  int ans = mod;
  for (int tim = 1; tim <= 100; ++tim) {
    int now = 0;
    for (int i = 1; i <= n; ++i) f[i] = i;
    edge.clear();
    for (int i = 1; i <= n; ++i) siz[i] = 0;
    random_shuffle(begin(tmp), end(tmp));
    while ((int)edge.size() < n - 1) {
      for (auto& p : tmp) {
        if (p.first <= k && siz[p.first] == d[p.first]) continue;
        if (p.second <= k && siz[p.second] == d[p.second]) continue;
        if (p.first <= k && p.second <= k && siz[p.first] == d[p.first] - 1 &&
            siz[p.second] == d[p.second] - 1)
          continue;
        int x = fa(p.first), y = fa(p.second);
        if (x == y) continue;
        f[x] = y;
        now += a[p.first][p.second];
        ++siz[p.first];
        ++siz[p.second];
        edge.insert(p);
      }
    }
    a[0][0] = -mod;
    int cnt0 = 0;
    for (;;) {
      for (int i = 1; i <= n; ++i) g[i].clear();
      for (auto& p : edge)
        g[p.first].push_back(p.second), g[p.second].push_back(p.first);
      for (int i = 1; i <= n; ++i) siz[i] = g[i].size();
      dif = mod;
      for (int i = 1; i <= n; ++i) {
        dfs(i, 0);
        for (int j = 1; j <= n; ++j) {
          int kk = j;
          while (kk != i) {
            bool flag = 0;
            --siz[kk];
            --siz[f2[kk]];
            ++siz[i];
            ++siz[j];
            for (int p = 1; p <= k; ++p)
              if (siz[p] > d[p]) {
                flag = 1;
                break;
              }
            ++siz[kk];
            ++siz[f2[kk]];
            --siz[i];
            --siz[j];
            if (!flag && dif >= a[i][j] - a[kk][f2[kk]]) {
              dif = a[i][j] - a[kk][f2[kk]];
              del = make_pair(min(kk, f2[kk]), max(kk, f2[kk]));
              ad = make_pair(min(i, j), max(i, j));
            }
            kk = f2[kk];
          }
        }
      }
      if (dif <= 0) {
        if (dif == 0)
          ++cnt0;
        else
          cnt0 = 0;
        if (cnt0 == 10) break;
        now += dif;
        edge.erase(del);
        edge.insert(ad);
      } else
        break;
    }
    ans = min(ans, now);
  }
  printf("%d\n", ans);
  return 0;
}

```