#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct node {
  int u, v, w;
  bool operator<(const node& o) const { return w < o.w; }
} e[N];
int n, m, a[N], f[N];
long long sv[N], me[N];
bool vis[N];
int getf(int x) { return f[x] == x ? x : f[x] = getf(f[x]); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) f[i] = i, sv[i] = a[i];
  for (int i = 1; i <= m; i++) scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
  sort(e + 1, e + 1 + m);
  int ans = m, now = 0;
  for (int i = 1; i <= m; i++) {
    int fu = getf(e[i].u), fv = getf(e[i].v);
    if (fu == fv) {
      if (sv[fu] < me[fu]) now--;
      me[fu] = max(me[fu], (long long)e[i].w);
      if (sv[fu] < me[fu]) now++;
    } else {
      if (sv[fu] < me[fu]) now--;
      if (sv[fv] < me[fv]) now--;
      f[fu] = fv;
      sv[fv] += sv[fu];
      me[fv] = max(me[fv], me[fu]);
      me[fv] = max(me[fv], (long long)e[i].w);
      if (sv[fv] < me[fv]) now++;
    }
    if (now == 0) ans = min(ans, m - i);
  }
  printf("%d\n", ans);
}
