#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
class unionFind {
 private:
  int* p;
  int* rank;
  int N;
  int* check;

 public:
  unionFind(int);
  int parent(int);
  void unit(int, int);
  int operator[](int);
  ~unionFind();
  int size(int n);
};
unionFind::unionFind(int n) {
  N = n;
  p = new int[N];
  rank = new int[N];
  for (int i = 0; i < N; i++) {
    p[i] = i;
    rank[i] = 0;
  }
  check = new int[N];
  for (int n = 0; n < N; n++) check[n] = 1;
  return;
}
int unionFind::parent(int n) {
  if (n < 0 || n >= N) return -1;
  if (p[n] == n) return n;
  return p[n] = parent(p[n]);
}
int unionFind::operator[](int n) { return parent(n); }
void unionFind::unit(int x, int y) {
  x = parent(x), y = parent(y);
  if (x == y) return;
  int sum = check[x] + check[y];
  if (rank[x] < rank[y])
    p[x] = y;
  else {
    p[y] = x;
    if (rank[x] == rank[y]) rank[x]++;
  }
  check[x] = sum;
  check[y] = sum;
  return;
}
unionFind::~unionFind() {
  delete (p);
  delete (rank);
  delete (check);
  return;
}
int unionFind::size(int n) { return check[parent(n)]; }
long long N, M;
vector<long long> X;
struct edge {
  long long src, to;
  long long w;
};
vector<edge> edges;
bool judge(long long erase) {
  unionFind UF(N);
  for (long long n = erase; n < M; n++) {
    UF.unit(edges[n].src, edges[n].to);
  }
  vector<long long> vec(N, 0);
  for (long long n = 0; n < N; n++) {
    vec[UF[n]] += X[n];
  }
  for (long long m = erase; m < M; m++) {
    if (vec[UF[edges[m].src]] < edges[m].w) {
      return false;
    }
  }
  return true;
}
int main() {
  cin >> N >> M;
  X.resize(N);
  for (long long n = 0; n < N; n++) cin >> X[n];
  edges.resize(M);
  for (long long m = 0; m < M; m++) {
    cin >> edges[m].src >> edges[m].to >> edges[m].w;
    edges[m].src--;
    edges[m].to--;
  }
  sort(edges.begin(), edges.end(), [](edge a, edge b) { return a.w > b.w; });
  long long s = -1, e = M;
  while (e - s > 1) {
    long long m = (e + s) / 2;
    if (judge(m))
      e = m;
    else
      s = m;
  }
  cout << e << endl;
  return 0;
}
