//For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
    #include <bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <cstring>
    #include <chrono>
    #include <complex>
    #define ll long long
    #define ld long double
    #define vi vector<int>
    #define vll vector<ll>
    #define vvi vector < vi >
    #define pii pair<int,int>
    #define pll pair<long long, long long>
    #define mod 1000000007
    #define inf 1000000000000000001;
    #define all(c) c.begin(),c.end()
    #define mp(x,y) make_pair(x,y)
    #define mem(a,val) memset(a,val,sizeof(a))
    #define eb emplace_back
    #define pb push_back
    #define f first
    #define s second
    
    using namespace std;


    // Disjoint Set DataStructure
    int parent[100001];         // reserve space for map if n > 10000
    int urank[100001];
    ll sum[100001],ar[100001];
    ll sz[100001],nt[100001];

    void create(ll x)
    {
        parent[x] = x ;
        urank[x] = 1 ;         // rank = no. of nodes in its subtree
        sz[x]=0;
        nt[x]=0;
        sum[x]=ar[x];
    }

    ll find(ll x)
    {
        if( parent[x] != x )    //path compression
        {
            parent[x] = find(parent[x]) ;
        }
        return parent[x];
    }

    void merge(ll x, ll y,ll z)
    {
        ll xroot = find(x);
        ll yroot = find(y);

        if( urank[xroot] <= urank[yroot] )    // Union by rank
        {
            parent[xroot] = yroot ;
            urank[yroot] = urank[yroot] + urank[xroot] ;
            sum[yroot]+=sum[xroot];
            nt[yroot]+=nt[xroot];
            sz[yroot]+=sz[xroot]+1;
            //cout<<sum[yroot]<<"............"<<z<<endl;
            if(sum[yroot]>=z)
                nt[yroot]=0;
            else
                ++nt[yroot];
        }
        else
        {
            //cout<<sum[xroot]<<"::::"<<sum[yroot]<<endl;
            parent[yroot] = xroot;
            urank[xroot] = urank[xroot] + urank[yroot] ;
            sum[xroot]+=sum[yroot];
            nt[xroot]+=nt[yroot];
            sz[xroot]+=sz[yroot]+1;
            //cout<<sum[xroot]<<"............"<<z<<endl;
            if(sum[xroot]>=z)
                nt[xroot]=0;
            else
                ++nt[xroot];
        }
    }

    class triple
    {
    public:
        int a,b,c;
    };

    bool comp(triple i,triple j)
    {
        return i.c<j.c;
    }

    vector<triple> v;

    int main()
    {
        std::ios::sync_with_stdio(false);
        int T;
        T=1;
        // cin.ignore(); must be there when using getline(cin, s)
        while(T--)
        {
            int n,m,i,j,a,b,c,x,y;
            cin>>n>>m;
            for(i=1;i<=n;++i)
            {
                cin>>ar[i];
                create(i);
            }
            for(i=1;i<=m;++i)
            {
                cin>>a>>b>>c;
                v.pb({a,b,c});
            }
            sort(v.begin(),v.end(),comp);

            for(i=0;i<v.size();++i)
            {
                x=find(v[i].a);
                y=find(v[i].b);
                //cout<<x<<" "<<y<<" "<<sum[x]<<" "<<nt[x]<<" "<<v[i].c<<endl;
                if(x==y)
                {
                    ++sz[x];
                    if(v[i].c<=sum[x])
                    {
                        nt[x]=0;
                    }
                    else
                        ++nt[x];
                }
                else
                {
                    merge(x,y,v[i].c);
                }
                //cout<<x<<" "<<y<<" "<<nt[x]<<" "<<nt[y]<<endl;
            }
            set<int> st;
            for(i=1;i<=n;++i)
            {
                st.insert(find(i));
            }
            int ans=0;
            for(auto it:st)
            {
                //cout<<it<<endl;
                ans+=nt[it];
            }
            cout<<ans<<endl;
        }
        return 0;
    }


Measured memory_bytes: 5880