1556_H. DIY Tree

<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

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


Example output:
95


Tags: graphs
greedy
math
probabilities
Difficulty rating: 3300

Solution:
#include <bits/stdc++.h>
using namespace std;
int nw[52], d[52], sz[52][52], fa[52];
int getv(int x) { return x == fa[x] ? x : fa[x] = getv(fa[x]); }
bool merge(int x, int y) {
  x = getv(x);
  y = getv(y);
  if (x == y) return false;
  fa[x] = y;
  return true;
}
int random(int n) {
  int he = 0;
  for (int i = 1; i <= n; i++) nw[i] = 0;
  vector<pair<int, int> > ve;
  for (int i = 1; i < n; i++) {
    he += sz[i][n], nw[i] = 1;
    ve.push_back(make_pair(i, n));
  }
  int ans = he;
  nw[n] = n - 1;
  double T = 100 * n;
  while (T > 1e-5) {
    int od = he, i = rand() % (n - 1);
    int oa = ve[i].first, ob = ve[i].second;
    nw[oa] -= 1;
    nw[ob] -= 1;
    he -= sz[oa][ob];
    ve.erase(ve.begin() + i);
    for (int i = 1; i <= n; i++) fa[i] = i;
    for (int i = 0; i < n - 2; i++) merge(ve[i].first, ve[i].second);
    int a, b;
    while (1) {
      a = rand() % (n - 1) + 1;
      b = rand() % (n - a) + a + 1;
      if (nw[a] < d[a] && nw[b] < d[b] && merge(a, b)) {
        he += sz[a][b];
        ve.push_back(make_pair(a, b));
        nw[a] += 1;
        nw[b] += 1;
        break;
      }
    }
    if (he < ans) ans = he;
    if (he > od && rand() % 10000 >= exp((od - he) / T) * 1e4) {
      he = od;
      ve.pop_back();
      ve.push_back(make_pair(oa, ob));
      nw[a] -= 1;
      nw[b] -= 1;
      nw[oa] += 1;
      nw[ob] += 1;
    }
    T *= 0.999995;
  }
  return ans;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; i++) scanf("%d", &d[i]);
  for (int i = k + 1; i <= n; i++) d[i] = 1e9;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      scanf("%d", &sz[i][j]);
      sz[j][i] = sz[i][j];
    }
  }
  int ans = random(n);
  printf("%d", ans);
  return 0;
}