#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-7;
namespace Simplex {
vector<int> X, Y;
vector<vector<double>> A;
vector<double> b, c;
double z;
int n, m;
void pivot(int x, int y) {
  swap(X[y], Y[x]);
  b[x] /= A[x][y];
  for (int i = 0, ThxDem = m; i < ThxDem; ++i)
    if (i != y) A[x][i] /= A[x][y];
  A[x][y] = 1 / A[x][y];
  for (int i = 0, ThxDem = n; i < ThxDem; ++i)
    if (i != x && abs(A[i][y]) > EPS) {
      b[i] -= A[i][y] * b[x];
      for (int j = 0, ThxDem = m; j < ThxDem; ++j)
        if (j != y) A[i][j] -= A[i][y] * A[x][j];
      A[i][y] = -A[i][y] * A[x][y];
    }
  z += c[y] * b[x];
  for (int i = 0, ThxDem = m; i < ThxDem; ++i)
    if (i != y) c[i] -= c[y] * A[x][i];
  c[y] = -c[y] * A[x][y];
}
pair<double, vector<double>> simplex(vector<vector<double>> _A,
                                     vector<double> _b, vector<double> _c) {
  A = _A;
  b = _b;
  c = _c;
  n = b.size();
  m = c.size();
  z = 0.;
  X = vector<int>(m);
  Y = vector<int>(n);
  for (int i = 0, ThxDem = m; i < ThxDem; ++i) X[i] = i;
  for (int i = 0, ThxDem = n; i < ThxDem; ++i) Y[i] = i + m;
  while (1) {
    int x = -1, y = -1;
    double mn = -EPS;
    for (int i = 0, ThxDem = n; i < ThxDem; ++i)
      if (b[i] < mn) mn = b[i], x = i;
    if (x < 0) break;
    for (int i = 0, ThxDem = m; i < ThxDem; ++i)
      if (A[x][i] < -EPS) {
        y = i;
        break;
      }
    if (y < 0) return {1, {}};
    pivot(x, y);
  }
  while (1) {
    double mx = EPS;
    int x = -1, y = -1;
    for (int i = 0, ThxDem = m; i < ThxDem; ++i)
      if (c[i] > mx) mx = c[i], y = i;
    if (y < 0) break;
    double mn = 1e200;
    for (int i = 0, ThxDem = n; i < ThxDem; ++i)
      if (A[i][y] > EPS && b[i] / A[i][y] < mn) mn = b[i] / A[i][y], x = i;
    assert(x >= 0);
    pivot(x, y);
  }
  vector<double> r(m);
  for (int i = 0, ThxDem = n; i < ThxDem; ++i)
    if (Y[i] < m) r[Y[i]] = b[i];
  return {z, r};
}
}  // namespace Simplex
const int MAX = 510;
int n;
long long d[MAX][MAX];
bool floyd_warshall() {
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  for (int i = 0; i < n; i++)
    if (d[i][i] < 0) return 1;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int x;
  cin >> n >> x;
  vector<int> color(n);
  int r = n;
  for (auto &i : color) cin >> i, r -= i;
  memset(d, LINF, sizeof d);
  for (int i = 1; i < n; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    a--, b--;
    d[a][b] = w, d[b][a] = w;
  }
  for (int i = 0; i < n; i++) d[i][i] = 0;
  floyd_warshall();
  vector<vector<double>> A;
  vector<double> b;
  vector<double> c;
  for (auto i : color) c.push_back(-(!i));
  for (int i = 0; i < n; i++) {
    b.push_back(-1);
    vector<double> restr;
    for (int j = 0; j < n; j++) restr.push_back(-(d[i][j] <= x));
    A.push_back(restr);
  }
  int black = n - r;
  A.push_back(vector<double>(n, 1));
  b.push_back(black);
  A.push_back(vector<double>(n, -1));
  b.push_back(-black);
  for (int i = 0; i < n; i++) {
    vector<double> restr(n, 0);
    restr[i] = 1;
    A.push_back(restr);
    b.push_back(1);
  }
  auto ans = Simplex::simplex(A, b, c);
  if (ans.first == 1)
    cout << -1 << endl;
  else
    cout << int(-ans.first + 0.5) << endl;
  exit(0);
}
