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: Wrong Answer)

#[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 merge(mut pool: Vec<(Cost, usize)>) -> Vec<(Cost, usize)> {
    if pool.len() == 0 { return pool; }
    pool.sort();
    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, anc, _), c) in dp2[par].iter() {
            pool.push(((d + pardist, anc, v), c));
        }
        pool.push(((pardist + x[par], par, v), 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);
    }
}

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 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::new();
        for i in 0 .. n {
            let ((cost, anc, dec), idx) = 
                if dp2[i][0].1 != color[i] {
                    dp2[i][0]
                } else {
                    dp2[i][1]
                };
            let cost = cost + x[i];
            edges.push(((cost, anc, dec), i, idx));
        }
        edges.sort();
        for ((cost, _, _), v, w) in edges {
            if !uf.is_same_set(v, w) {
                tot += cost;
                uf.unite(v, w);
                conn -= 1;
            }
        }
        assert_ne!(oldconn, conn);
    }
    puts!("{}\n", tot);
}

fn main() {
    // 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();
}