#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, cutted_edges, unsolved_points, now_color, a[N], block[N], visit[N];
pair<pair<int, int>, int> p[N];
vector<int> v[N];
map<pair<int, int>, int> mp;
map<pair<int, int>, int> stop;
bool cmp(pair<pair<int, int>, int> pa, pair<pair<int, int>, int> pb) {
  return pa.second > pb.second;
}
void dfs(int x) {
  visit[x] = now_color;
  block[now_color] += a[x];
  for (int i = 0; i < v[x].size(); i++)
    if (visit[v[x][i]] == -1 && stop[make_pair(v[x][i], x)] != 1) {
      dfs(v[x][i]);
    }
  return;
}
void check() {
  memset(visit, -1, sizeof(visit));
  memset(block, 0, sizeof(block));
  now_color = 0;
  for (int i = 1; i <= n; i++) {
    if (visit[i] == -1) {
      now_color++;
      dfs(i);
    }
  }
  int ret = 0;
  for (int i = cutted_edges + 1; i <= m; i++)
    if (block[visit[p[i].first.first]] < p[i].second)
      if (stop[make_pair(p[i].first.first, p[i].first.second)] != 1) {
        ret++;
        continue;
      }
  unsolved_points = ret;
  return;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    v[x].push_back(y);
    v[y].push_back(x);
    mp[make_pair(x, y)] = mp[make_pair(y, x)] = z;
    p[i].first.first = x;
    p[i].first.second = y;
    p[i].second = z;
  }
  sort(p + 1, p + m + 1, cmp);
  check();
  if (unsolved_points == 0) {
    cout << 0 << endl;
    return 0;
  }
  int limit = unsolved_points;
  for (int i = 1; i <= m; i++) {
    stop[make_pair(p[i].first.first, p[i].first.second)] =
        stop[make_pair(p[i].first.second, p[i].first.first)] = 1;
    cutted_edges = i;
    check();
    if (unsolved_points == 0) {
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}
