Problem:
Score : 1900 points

Problem Statement

Ringo has a tree with N vertices.
The i-th of the N-1 edges in this tree connects Vertex A_i and Vertex B_i and has a weight of C_i.
Additionally, Vertex i has a weight of X_i.

Here, we define f(u,v) as the distance between Vertex u and Vertex v, plus X_u + X_v.

We will consider a complete graph G with N vertices.
The cost of its edge that connects Vertex u and Vertex v is f(u,v).
Find the minimum spanning tree of G.

Constraints

2 \leq N \leq 200,000

1 \leq X_i \leq 10^9

1 \leq A_i,B_i \leq N

1 \leq C_i \leq 10^9

The given graph is a tree.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N
A_1 B_1 C_1
A_2 B_2 C_2
:
A_{N-1} B_{N-1} C_{N-1}

Output

Print the cost of the minimum spanning tree of G.

Sample Input 1

4
1 3 5 1
1 2 1
2 3 2
3 4 3

Sample Output 1

22

We connect the following pairs: Vertex 1 and 2, Vertex 1 and 4, Vertex 3 and 4. The costs are 5, 8 and 9, respectively, for a total of 22.

Sample Input 2

6
44 23 31 29 32 15
1 2 10
1 3 12
1 4 16
4 5 8
4 6 15

Sample Output 2

359

Sample Input 3

2
1000000000 1000000000
2 1 1000000000

Sample Output 3

3000000000

Solution in Rust (1.15.1) (judge verdict: Accepted)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::{Write, BufWriter};
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, [ $t:tt ]) => {{
        let len = read_value!($next, usize);
        (0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    }};

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

/**
 * Union-Find tree.
 * Verified by yukicoder No.94 (http://yukicoder.me/submissions/82111)
 */
struct UnionFind { disj: Vec<usize> }

impl UnionFind {
    fn new(n: usize) -> Self {
        let mut disj = vec![0; n];
        for i in 0 .. n {
            disj[i] = i;
        }
        UnionFind { disj: disj }
    }
    fn root(self: &mut Self, x: usize) -> usize {
        if x != self.disj[x] {
            let par = self.disj[x];
            let r = self.root(par);
            self.disj[x] = r;
        }
        return self.disj[x];
    }
    fn unite(self: &mut Self, x: usize, y: usize) {
        let xr = self.root(x);
        let yr = self.root(y);
        self.disj[xr] = yr;
    }
    fn is_same_set(self: &mut Self, x: usize, y: usize) -> bool {
        return self.root(x) == self.root(y);
    }
}

type Cost = (i64, usize, usize);

fn cmp(&((a, b, c), d): &(Cost, usize), &((a2, b2, c2), d2): &(Cost, usize)) -> Ordering {
    if a != a2 { return a.cmp(&a2); }
    let bc = if b > c { (c, b) } else { (b, c) };
    let bc2 = if b2 > c2 { (c2, b2) } else { (b2, c2) };
    if bc != bc2 { return bc.cmp(&bc2); }
    d.cmp(&d2)
}

fn merge(mut pool: Vec<(Cost, usize)>) -> Vec<(Cost, usize)> {
    if pool.len() == 0 { return pool; }
    pool.sort_by(cmp);
    let mut ans = vec![pool[0]];
    for &p in pool[1..].iter() {
        if p.1 != ans[0].1 {
            ans.push(p);
            return ans;
        }
    }
    ans
}

fn dfs1(v: usize, par: usize, x: &[i64], g: &[Vec<(usize, i64)>], color: &[usize], dp1: &mut [Vec<(Cost, usize)>]) {
    let mut pool = Vec::new();
    for &(w, cost) in g[v].iter() {
        if w == par { continue; }
        dfs1(w, v, x, g, color, dp1);
        for &((a, _, u), b) in dp1[w].iter() {
            pool.push(((a + cost, v, u), b));
        }
        pool.push(((cost + x[w], v, w), color[w]));
    }
    dp1[v] = merge(pool);
}

fn dfs2(v: usize, par: usize, x: &[i64], g: &[Vec<(usize, i64)>],
        color: &[usize],
        dp1: &[Vec<(Cost, usize)>], dp2: &mut [Vec<(Cost, usize)>],
        pardist: i64) {
    let mut pool = Vec::new();
    let n = g.len();
    if par < n {
        for &((d, _, dec), c) in dp2[par].iter() {
            pool.push(((d + pardist, v, dec), c));
        }
        pool.push(((pardist + x[par], v, par), color[par]));
    }
    pool.append(&mut dp1[v].clone());
    dp2[v] = merge(pool);

    for &(w, cost) in g[v].iter() {
        if w == par { continue; }
        dfs2(w, v, x, g, color, dp1, dp2, cost);
    }
}

const INF: i64 = 1 << 60;

fn calc(x: &[i64], g: &[Vec<(usize, i64)>]) -> i64 {
    let n = x.len();
    let mut uf = UnionFind::new(n);
    let mut conn = n;
    let mut tot: i64 = 0;
    while conn > 1 {
        let oldconn = conn;
        let mut color = vec![0; n];
        for i in 0 .. n { color[i] = uf.root(i); }
        let mut dp1 = vec![Vec::new(); n];
        let mut dp2 = vec![Vec::new(); n];
        dfs1(0, n, &x, &g, &color, &mut dp1);
        dfs2(0, n, &x, &g, &color, &dp1, &mut dp2, 0);
        // eprintln!("dp1 = {:?}", dp1);
        // eprintln!("dp2 = {:?}", dp2);
        let mut edges = vec![(INF, n, n); n];
        for i in 0 .. n {
            let ((cost, anc, dec), _idx) =
                if !uf.is_same_set((dp2[i][0].0).1, (dp2[i][0].0).2) {
                    dp2[i][0]
                } else {
                    dp2[i][1]
                };
            let cost = cost + x[i];
            let (anc, dec) = if anc > dec {
                (dec, anc)
            } else {
                (anc, dec)
            };
            assert_ne!(anc, dec);
            let re = &mut edges[uf.root(i)];
            *re = min(*re, (cost, anc, dec));
        }
        edges.sort();
        for (cost, anc, dec) in edges {
            if cost < INF && !uf.is_same_set(anc, dec) {
                tot = tot.checked_add(cost).unwrap();
                uf.unite(anc, dec);
                conn -= 1;
            }
        }
        assert_ne!(oldconn, conn);
    }
    tot
}

fn dfs_naive(v: usize, par: usize, g: &[Vec<(usize, i64)>], d: i64,
             dp: &mut [i64]) {
    dp[v] = d;
    for &(w, cost) in g[v].iter() {
        if w == par { continue; }
        dfs_naive(w, v, g, d + cost, dp);
    }
}

fn calc_naive(x: &[i64], g: &[Vec<(usize, i64)>]) -> i64 {
    let n = x.len();
    let mut uf = UnionFind::new(n);
    let mut tot = 0;
    let mut dist = vec![vec![0; n]; n];
    for i in 0 .. n {
        dfs_naive(i, n, g, 0, &mut dist[i]);
    }
    let mut edges = Vec::new();
    for i in 0 .. n {
        for j in 0 .. i {
            edges.push((x[i] + x[j] + dist[i][j], i, j));
        }
    }
    edges.sort();
    for (a, b, c) in edges {
        assert!(a > 0);
        if !uf.is_same_set(b, c) {
            tot += a;
            uf.unite(b, c);
        }
    }
    tot
}
fn solve() {
    let out = std::io::stdout();
    let mut out = BufWriter::new(out.lock());
    macro_rules! puts {
        ($($format:tt)*) => (write!(out,$($format)*).unwrap());
    }
    input! {
        n: usize,
        x: [i64; n],
        abc: [(usize1, usize1, i64); n - 1],
    }
    let mut g = vec![Vec::new(); n];
    for (a, b, c) in abc {
        g[a].push((b, c));
        g[b].push((a, c));
    }
    let tot = calc(&x, &g);
    // let tot_naive = calc_naive(&x, &g);
    // assert_eq!(tot, tot_naive);
    puts!("{}\n", tot);
}

fn main() {
    // random test
    let n = 10000;
    let a = 123456i64;
    let b = 12345i64;
    let mut seed = 111i64;
    let mut rnd = || {
        seed = seed.wrapping_mul(a).wrapping_add(b);
        (seed >> 10) & 0xffffffff
    };
    for _ in 0 .. 0 {
        let mut x = vec![0; n];
        for i in 0 .. n { x[i] = rnd() & 0xffffffff; }
        let mut g = vec![Vec::new(); n];
        for i in 1 .. n {
            let w = rnd();
            let par = rnd() as usize % i;
            g[i].push((par, w));
            g[par].push((i, w));
        }
        let tot = calc(&x, &g);
        let tot_naive = calc_naive(&x, &g);
        assert_eq!(tot, tot_naive);
    }
    // In order to avoid potential stack overflow, spawn a new thread.
    let stack_size = 104_857_600; // 100 MB
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}