#include <bits/stdc++.h>
using namespace std;
int fa[100010];
long long weight[100010];
struct node {
  int a, b, w;
  bool operator<(const node& x) const { return w < x.w; }
} edge[100010];
int ans, cnt;
int is_not[100010];
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
int n, m;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> weight[i], fa[i] = i;
  for (int i = 1; i <= m; ++i) {
    int a, b, y;
    cin >> a >> b >> y;
    edge[i] = (node){a, b, y};
  }
  sort(edge + 1, edge + m + 1);
  ans = m;
  for (int i = 1; i <= m; ++i) {
    int cur = edge[i].w, u = edge[i].a, v = edge[i].b;
    if (getfa(u) == getfa(v)) {
      int father = getfa(u);
      if (weight[father] < cur && !is_not[father]) ++cnt, is_not[father] = 1;
    } else {
      int pre = 0;
      pre = is_not[(getfa(u))] + is_not[getfa(v)];
      is_not[getfa(u)] = is_not[getfa(v)] = 0;
      weight[getfa(v)] += weight[getfa(u)];
      fa[getfa(u)] = getfa(v);
      if (weight[getfa(u)] < cur) ++cnt, is_not[getfa(u)] = 1;
      cnt -= pre;
    }
    if (cnt == 0) ans = m - i;
  }
  cout << ans;
  return 0;
}
