#include <bits/stdc++.h>
using namespace std;
int n, m;
long x[1 << 17];
int cnt = 0;
struct UF {
  int n;
  vector<int> parent, rank;
  vector<long> w, mw;
  UF(int n_ = 0) : n(n_), parent(n_), rank(n_, 0), w(n_), mw(n_, 0) {
    for (int i = 0; i < n_; i++) {
      parent[i] = i;
      w[i] = x[i];
    }
  }
  int find(int a) { return parent[a] != a ? parent[a] = find(parent[a]) : a; }
  bool same(int a, int b) { return find(a) == find(b); }
  bool unite(int a, int b) {
    a = find(a), b = find(b);
    if (a == b) return false;
    if (rank[a] < rank[b]) {
      parent[a] = b;
      cnt -= w[a] < mw[a];
      cnt -= w[b] < mw[b];
      w[b] += w[a];
      mw[b] = max(mw[b], mw[a]);
      cnt += w[b] < mw[b];
    } else {
      parent[b] = a;
      cnt -= w[a] < mw[a];
      cnt -= w[b] < mw[b];
      w[a] += w[b];
      mw[a] = max(mw[a], mw[b]);
      cnt += w[a] < mw[a];
      if (rank[a] == rank[b]) rank[a]++;
    }
    return true;
  }
};
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> x[i];
  UF uf(n);
  vector<pair<long, pair<int, int> > > G;
  for (int i = 0; i < m; i++) {
    long y;
    int u, v;
    cin >> u >> v >> y;
    G.push_back(make_pair(y, make_pair(u - 1, v - 1)));
  }
  sort(G.begin(), G.end());
  int ans = 0;
  for (int i = 0; i < m; i++) {
    int u = uf.find(G[i].second.first);
    cnt -= uf.w[u] < uf.mw[u];
    uf.mw[u] = max(uf.mw[u], G[i].first);
    cnt += uf.w[u] < uf.mw[u];
    uf.unite(G[i].second.first, G[i].second.second);
    if (cnt == 0) ans = m - i - 1;
  }
  cout << ans << endl;
}
