#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chmax(T &a, T b) {
  a = max(a, b);
}
template <typename T>
void chmin(T &a, T b) {
  a = min(a, b);
}
template <typename T>
void chadd(T &a, T b) {
  a = a + b;
}
long long int dx[] = {0, 0, 1, -1};
long long int dy[] = {1, -1, 0, 0};
const long long INF = 1001001001001001LL;
const long long MOD = 1000000007LL;
struct UnionFind {
 private:
  const long long int n;
  long long int __size;

 public:
  vector<long long int> uf, pot;
  UnionFind(long long int _n, vector<long long int> pot_)
      : n(_n), __size(_n), uf(_n, -1), pot(pot_) {}
  long long int find(long long int x) {
    return (uf[x] < 0) ? x : uf[x] = find(uf[x]);
  }
  bool same(long long int x, long long int y) { return find(x) == find(y); }
  long long int size(long long int x) { return -uf[find(x)]; }
  long long int size() { return __size; }
  void unite(long long int x, long long int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    __size--;
    if (uf[y] < uf[x]) swap(x, y);
    uf[x] += uf[y];
    pot[x] += pot[y];
    uf[y] = x;
  }
};
long long int N, M;
signed main() {
  cin >> N >> M;
  vector<long long int> X(N);
  for (long long int i = 0; i < N; i++) {
    cin >> X[i];
  }
  vector<tuple<long long int, long long int, long long int> > edges;
  for (long long int i = 0; i < M; i++) {
    long long int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    edges.emplace_back(w, u, v);
  }
  sort(edges.begin(), edges.end());
  UnionFind uf(N, X);
  vector<tuple<long long int, long long int, long long int> > tmp;
  long long int ans = INF;
  for (long long int i = 0; i < M; i++) {
    long long int pre = get<0>(edges[i]);
    while (i < M and pre == get<0>(edges[i])) {
      tmp.emplace_back(edges[i]);
      i++;
    }
    for (auto e : tmp) {
      long long int w, u, v;
      tie(w, u, v) = e;
      uf.unite(u, v);
    }
    bool ok = true;
    for (auto e : tmp) {
      long long int w, u, v;
      tie(w, u, v) = e;
      long long int id = uf.find(u);
      ok &= (w <= uf.pot[id]);
    }
    tmp.clear();
    if (ok) {
      ans = min(ans, M - i);
    }
    i--;
  }
  cout << ans << endl;
  return 0;
}
