fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    let n: usize = sc.read();
    let m: usize = sc.read();
    let x: Vec<i64> = sc.vec(n);
    let mut edges = vec![];
    let mut costs = vec![];
    for i in 0..m {
        let a = sc.read::<usize>() - 1;
        let b = sc.read::<usize>() - 1;
        let cost: i64 = sc.read();
        costs.push((cost, i));
        edges.push((a, b, cost));
    }

    let mut uf = UnionFind::new(n, &x);
    let mut ok = vec![false; m];
    costs.sort();
    costs.reverse();
    while let Some(&(cost, _)) = costs.last() {
        let mut e = vec![];
        while let Some((c, edge)) = costs.pop() {
            if c == cost {
                e.push(edge);
            } else {
                costs.push((c, edge));
                break;
            }
        }

        for &i in e.iter() {
            let (a, b, _) = edges[i];
            uf.unite(a, b);
        }

        for &i in e.iter() {
            let (a, _, _) = edges[i];
            let root = uf.find(a);
            let sum = uf.sum[root];
            if sum >= cost {
                ok[i] = true;
            }
        }
    }

    let mut costs = edges
        .iter()
        .enumerate()
        .map(|(i, &(_, _, cost))| (cost, i))
        .collect::<Vec<_>>();
    costs.sort();
    costs.reverse();

    let mut graph = vec![vec![]; n];
    for (i, &(a, b, cost)) in edges.iter().enumerate() {
        graph[a].push((b, cost, i));
        graph[b].push((a, cost, i));
    }

    let mut vis = vec![false; m];
    for (_, i) in costs.into_iter() {
        if !ok[i] {
            continue;
        }
        let (a, b, cost) = edges[i];
        vis[i] = true;
        dfs(a, &graph, &mut vis, cost);
        dfs(b, &graph, &mut vis, cost);
    }

    let remove = vis.into_iter().filter(|&v| !v).count();
    println!("{}", remove);
}

fn dfs(v: usize, graph: &Vec<Vec<(usize, i64, usize)>>, vis: &mut Vec<bool>, cost: i64) {
    for &(next, edge_cost, edge_id) in graph[v].iter() {
        if vis[edge_id] {
            continue;
        }
        if edge_cost > cost {
            continue;
        }
        vis[edge_id] = true;
        dfs(next, graph, vis, cost);
    }
}

pub struct UnionFind {
    parent: Vec<usize>,
    sum: Vec<i64>,
    sizes: Vec<usize>,
    size: usize,
}

impl UnionFind {
    pub fn new(n: usize, v: &Vec<i64>) -> UnionFind {
        UnionFind {
            parent: (0..n).map(|i| i).collect::<Vec<usize>>(),
            sum: v.clone(),
            sizes: vec![1; n],
            size: n,
        }
    }

    pub fn find(&mut self, x: usize) -> usize {
        if x == self.parent[x] {
            x
        } else {
            let px = self.parent[x];
            self.parent[x] = self.find(px);
            self.parent[x]
        }
    }

    pub fn unite(&mut self, x: usize, y: usize) -> bool {
        let parent_x = self.find(x);
        let parent_y = self.find(y);
        if parent_x == parent_y {
            return false;
        }

        let (large, small) = if self.sizes[parent_x] < self.sizes[parent_y] {
            (parent_y, parent_x)
        } else {
            (parent_x, parent_y)
        };

        self.parent[small] = large;

        self.sizes[large] += self.sizes[small];
        self.sizes[small] = 0;

        self.sum[large] += self.sum[small];
        self.sum[small] = 0;

        self.size -= 1;
        return true;
    }
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> Self {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}


Measured memory_bytes: 32988