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<u64> = sc.vec(n);

    let edges = (0..m)
        .map(|_| {
            let a = sc.read::<usize>() - 1;
            let b = sc.read::<usize>() - 1;
            let c = sc.read::<u64>();
            (a, b, c)
        })
        .collect::<Vec<_>>();
    let mut e = edges
        .iter()
        .enumerate()
        .map(|(i, &(a, b, c))| (c, a, b, i))
        .collect::<Vec<_>>();
    e.sort();
    e.reverse();
    let mut uf = UnionFind::new(n, &x);
    let mut remove_candidate = vec![false; m];
    while let Some((c, a, b, i)) = e.pop() {
        let mut x = vec![(a, b, i)];
        while let Some((c0, a, b, i)) = e.pop() {
            if c0 == c {
                x.push((a, b, i));
            } else {
                e.push((c0, a, b, i));
                break;
            }
        }

        for &(a, b, _) in x.iter() {
            uf.unite(a, b);
        }
        for (a, _, i) in x.into_iter() {
            let root = uf.find(a);
            if c > uf.w[root] {
                remove_candidate[i] = true;
            }
        }
    }

    let mut graph = vec![vec![]; n];
    for i in 0..m {
        let (a, b, _) = edges[i];
        graph[a].push((b, i));
        graph[b].push((a, i));
    }

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

    let mut used = vec![false; m];
    for (_, edge_id) in e.into_iter() {
        if !remove_candidate[edge_id] {
            let (a, b, cost) = edges[edge_id];
            dfs(a, &mut used, cost, &graph, &edges);
            dfs(b, &mut used, cost, &graph, &edges);
        }
    }

    let used = used.into_iter().filter(|&b| b).count();
    println!("{}", m - used);
}

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

pub struct UnionFind {
    parent: Vec<usize>,
    sizes: Vec<usize>,
    size: usize,
    w: Vec<u64>,
}

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

    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.size -= 1;

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

        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) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: std::ops::Deref<Target = str>>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.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: 35068