#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
long long a[100005];
vector<long long> com[100005];
vector<pair<pair<long long, long long>, pair<long long, long long> > >
    edge[100005];
bool vis[100005], vis1[100005];
vector<int> temp;
int cc;
map<pair<long long, long long>, long long> mp, mq, viss;
long long ans2 = 0, mx = 0;
void dfs(int s) {
  vis[s] = 1;
  int i;
  com[cc].push_back(s);
  for (i = 0; i < v[s].size(); i++) {
    if (!vis[v[s][i]]) {
      dfs(v[s][i]);
    }
  }
}
void dfs2(int s, int idx) {
  vis1[s] = true;
  int i;
  ans2 += a[s];
  for (i = 0; i < v[s].size(); i++) {
    if (mq[{s, v[s][i]}] >= idx) {
      return;
    }
    if (!vis1[v[s][i]]) {
      mx = max(mx, mp[{s, v[s][i]}]);
      dfs2(v[s][i], idx);
    }
  }
}
long long check(long long pp, long long idx, long long wt) {
  int flag = 0;
  int i;
  for (i = 0; i < com[idx].size(); i++) {
    if (!vis1[com[idx][i]]) {
      dfs2(com[idx][i], pp);
      if (ans2 > mx) {
        mx = 0;
        ans2 = 0;
      } else {
        flag = 1;
        break;
      }
    }
  }
  for (i = 0; i < com[idx].size(); i++) {
    vis1[com[idx][i]] = 0;
  }
  if (flag == 1) {
    return 0;
  }
  return 1;
}
int main() {
  int n, m;
  cin >> n >> m;
  int i;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  long long aa, bb, c;
  for (i = 1; i <= m; i++) {
    cin >> aa >> bb >> c;
    v[aa].push_back(bb);
    v[bb].push_back(aa);
    mp[{aa, bb}] = c;
    mp[{bb, aa}] = c;
  }
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      cc++;
      dfs(i);
    }
  }
  for (i = 1; i <= cc; i++) {
    for (int j = 0; j < com[i].size(); j++) {
    }
  }
  int j, k;
  for (i = 1; i <= cc; i++) {
    for (j = 0; j < com[i].size(); j++) {
      for (k = 0; k < v[com[i][j]].size(); k++) {
        long long g = com[i][j];
        long long h = v[g][k];
        if (viss[{g, h}] == 0) {
          edge[i].push_back(
              make_pair(make_pair(mp[{g, h}], i), make_pair(g, h)));
          viss[{g, h}] = 1;
          viss[{h, g}] = 1;
        }
      }
    }
  }
  for (i = 1; i <= cc; i++) {
    sort(edge[i].begin(), edge[i].end());
  }
  for (i = 1; i <= cc; i++) {
    for (j = 0; j < edge[i].size(); j++) {
      edge[i][j].first.second = j;
      mq[{edge[i][j].second.first, edge[i][j].second.second}] = j;
    }
  }
  long long ans = 0, ans1 = 0;
  for (i = 1; i <= cc; i++) {
    ans = 0;
    long long low = 0, high = edge[i].size() - 1;
    while (low <= high) {
      long long mid = (low + high) / 2;
      cout << mid << " " << edge[i][mid].first.first << endl;
      if (check(mid, i, edge[i][mid].first.first)) {
        ans = edge[i].size() - mid;
        high = mid - 1;
      } else {
        low = mid + 1;
      }
    }
    ans1 += ans;
  }
  cout << ans1 << endl;
}
