#include <bits/stdc++.h>
using namespace std;
string char_to_str(char c) {
  string tem(1, c);
  return tem;
}
long long a[100001];
long long dsu[100001];
long long size[100001];
long long med[100001];
long long suw[100001];
long long root(long long i) {
  if (dsu[i] == i) return i;
  dsu[i] = root(dsu[i]);
  return dsu[i];
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) cin >> a[i];
  pair<long long, pair<long long, long long>> ed[m];
  for (long long i = 0; i < m; ++i) {
    long long x, y, w;
    cin >> x >> y >> w;
    ed[i].second.first = x;
    ed[i].second.second = y;
    ed[i].first = w;
  }
  sort(ed, ed + m);
  long long valid[n + 1];
  for (long long i = 1; i <= n; ++i) {
    dsu[i] = i;
    size[i] = 1;
    valid[i] = 1;
    med[i] = 0;
    suw[i] = a[i];
  }
  long long inv = 0, ans = m;
  for (long long i = 0; i < m; ++i) {
    long long x, y, w;
    x = ed[i].second.first;
    y = ed[i].second.second;
    w = ed[i].first;
    long long rx = root(x);
    long long ry = root(y);
    if (rx == ry) {
      med[rx] = max(med[rx], w);
      if (valid[rx] == 1) {
        if (med[rx] > suw[rx]) {
          valid[rx] = 0;
          inv++;
        }
      }
    } else {
      if (valid[rx] == 0) inv--;
      if (valid[ry] == 0) --inv;
      if (size[rx] < size[ry]) swap(rx, ry);
      dsu[ry] = rx;
      suw[rx] += suw[ry];
      med[rx] = max(med[rx], med[ry]);
      size[rx] += size[ry];
      med[rx] = max(med[rx], w);
      if (med[rx] > suw[rx]) {
        valid[rx] = 0;
        ++inv;
      } else
        valid[rx] = 1;
    }
    if (inv == 0) ans = m - i - 1;
  }
  cout << ans;
}
