#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 30;
const ll LINF = 1LL << 60;
const double eps = 1e-5;
const ll MOD = 1000000007LL;
template <typename T>
void chmin(T &a, T b) {
  a = min(a, b);
};
template <typename T>
void chmax(T &a, T b) {
  a = max(a, b);
};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
struct UnionFind {
  vector<int> par;
  vector<int> dep;
  UnionFind(int siz) {
    par.assign(siz, 0);
    dep.assign(siz, 0);
    for (int i = 0; i < siz; i++) {
      par[i] = i;
    }
  };
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return false;
    }
    if (dep[x] < dep[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (dep[x] == dep[y]) {
        dep[x]++;
      }
    }
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<ll> x(n);
  for (int i = 0; i < n; i++) cin >> x[i];
  vector<pair<P, P>> v;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    ll y;
    cin >> y;
    v.push_back(make_pair(P(x[a] + x[b] - y, y), P(a, b)));
  }
  sort(v.begin(), v.end());
  int ok = 0, ng = m;
  while (ng - ok > 1) {
    int mid = (ok + ng) / 2;
    UnionFind uf(n);
    for (int i = mid; i < m; i++) {
      int a = v[i].second.first, b = v[i].second.second;
      uf.unite(a, b);
    }
    bool f = true;
    vector<ll> sum(n);
    for (int i = 0; i < n; i++) {
      sum[uf.find(i)] += x[i];
    }
    for (int i = mid; i < m; i++) {
      int a = v[i].second.first;
      ll y = v[i].first.second;
      if (sum[uf.find(a)] < y) f = false;
    }
    if (f) {
      ng = mid;
    } else {
      ok = mid;
    }
  }
  cout << ng << endl;
}
