#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> x(n);
  for (int i = (0); i < (n); ++i) cin >> x[i];
  vector<pair<int, pair<int, int>>> yab(m);
  for (int i = (0); i < (m); ++i) {
    cin >> yab[i].second.first >> yab[i].second.second >> yab[i].first;
    --yab[i].second.first;
    --yab[i].second.second;
  }
  sort((yab).begin(), (yab).end());
  reverse((yab).begin(), (yab).end());
  int mn = -1, mx = m;
  while (mx - mn > 1) {
    int mid = (mn + mx) / 2;
    vector<vector<pair<int, int>>> edge(n);
    for (int i = (mid); i < (m); ++i) {
      edge[yab[i].second.first].push_back({yab[i].second.second, yab[i].first});
      edge[yab[i].second.second].push_back({yab[i].second.first, yab[i].first});
    }
    vector<bool> used(n, false);
    bool ok = true;
    for (int i = (0); i < (n); ++i)
      if (!used[i]) {
        long long ver = x[i];
        int weight = 0;
        used[i] = true;
        queue<int> que;
        que.push(i);
        while (!que.empty()) {
          int fr = que.front();
          que.pop();
          for (auto next : edge[fr]) {
            if (used[next.first]) {
              weight = max(weight, next.second);
            } else {
              used[next.first] = true;
              ver += x[next.first];
              weight = max(weight, next.second);
              que.push(next.first);
            }
          }
        }
        if (ver < weight) {
          ok = false;
          break;
        }
      }
    (ok ? mx : mn) = mid;
  }
  cout << mx << '\n';
  return 0;
}
