Problem:
Score : 1100 points

Problem Statement

There is a tree with N vertices, numbered 1 through N.
The i-th edge in this tree connects Vertices A_i and B_i and has a length of C_i.

Joisino created a complete graph with N vertices.
The length of the edge connecting Vertices u and v in this graph, is equal to the shortest distance between Vertices u and v in the tree above.

Joisino would like to know the length of the longest Hamiltonian path (see Notes) in this complete graph.
Find the length of that path.

Notes

A Hamiltonian path in a graph is a path in the graph that visits each vertex exactly once.

Constraints

2 \leq N \leq 10^5

1 \leq A_i < B_i \leq N

The given graph is a tree.

1 \leq C_i \leq 10^8

All input values are integers.

Input

Input is given from Standard Input in the following format:

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 length of the longest Hamiltonian path in the complete graph created by Joisino.

Sample Input 1

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

Sample Output 1

38

The length of the Hamiltonian path 5 → 3 → 1 → 4 → 2 is 5+8+15+10=38. Since there is no Hamiltonian path with length 39 or greater in the graph, the answer is 38.

Sample Input 2

8
2 8 8
1 5 1
4 8 2
2 5 4
3 8 6
6 8 9
2 7 12

Sample Output 2

132

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

fn dfs(u: usize, prev: usize, g: &Vec<Vec<usize>>, sz: &mut Vec<usize>, centroid: &mut Vec<usize>) {
    sz[u] = 1;
    let mut is_centroid = true;
    for &v in g[u].iter() {
        if v != prev {
            dfs(v, u, g, sz, centroid);
            sz[u] += sz[v];
            if sz[v] > g.len() / 2 {
                is_centroid = false;
            }
        }
    }
    if g.len() - sz[u] > g.len() / 2 {
        is_centroid = false;
    }
    if is_centroid {
        centroid.push(u);
    }
}

fn centroid(g: &Vec<Vec<usize>>) -> Vec<usize> {
    let mut centroid = Vec::new();
    let mut sz = vec![0usize; g.len()];
    dfs(0, std::usize::MAX, g, &mut sz, &mut centroid);
    centroid
}

fn make_tree(
    cur_idx: usize,
    parent_idx: usize,
    edges: &Vec<Vec<usize>>,
    tree: &mut Vec<Vec<usize>>,
    parent_dict: &mut HashMap<usize, usize>,
) {
    for child_idx in edges[cur_idx].iter() {
        if *child_idx == parent_idx {
            continue;
        }
        tree[cur_idx].push(*child_idx);
        parent_dict.insert(*child_idx, cur_idx);
        make_tree(*child_idx, cur_idx, edges, tree, parent_dict);
    }
}

fn topological_dfs(cur_idx: usize, tree: &Vec<Vec<usize>>, result: &mut Vec<usize>) {
    for child_idx in tree[cur_idx].iter() {
        result.push(*child_idx);
        topological_dfs(*child_idx, tree, result);
    }
}

fn main() {
    let n: usize = read();
    let mut edges = HashMap::<usize, HashMap<usize, u64>>::new();
    let mut simple_edges = vec![Vec::new(); n];
    for i in 0..n - 1 {
        let v = read_vec::<u64>();
        let a = v[0] as usize - 1;
        let b = v[1] as usize - 1;
        edges.entry(a).or_insert(HashMap::new()).insert(b, v[2]);
        edges.entry(b).or_insert(HashMap::new()).insert(a, v[2]);
        // edges[a][b] = v[2];
        // edges[b][a] = v[2];
        simple_edges[a].push(b);
        simple_edges[b].push(a);
    }
    if n == 2 {
        println!("{}", edges[&0][&1]);
        return;
    }

    let centroid = centroid(&simple_edges);
    if centroid.len() == 1 {
        let root = centroid[0];
        let mut tree: Vec<Vec<usize>> = vec![Vec::new(); n];
        let mut parent_dict: HashMap<usize, usize> = HashMap::new();
        make_tree(root, n, &simple_edges, &mut tree, &mut parent_dict);
        let mut topological_sorted_indexes = vec![root];
        topological_dfs(root, &tree, &mut topological_sorted_indexes);

        let mut rank = vec![0u64; n];
        for &i in topological_sorted_indexes.iter().rev() {
            if tree[i].is_empty() {
                rank[i] = 1;
                continue;
            }
            rank[i] = tree[i].iter().map(|&x| rank[x]).sum::<u64>() + 1;
        }

        let mut lightest_end = (0..n)
            .map(|x| {
                if x == root {
                    std::u64::MAX
                } else {
                    edges[&x][&parent_dict[&x]]
                }
            })
            .min()
            .unwrap();
        let mut ans = (0..n)
            .map(|x| {
                if x == root {
                    0
                } else {
                    2 * rank[x] * edges[&x][&parent_dict[&x]]
                }
            })
            .sum::<u64>();
        ans -= lightest_end;
        println!("{}", ans);
    } else {
        let mut rank = vec![0u64; n];
        let root = centroid[0];
        let mut tree: Vec<Vec<usize>> = vec![Vec::new(); n];
        let mut parent_dict: HashMap<usize, usize> = HashMap::new();
        make_tree(root, n, &simple_edges, &mut tree, &mut parent_dict);
        let mut topological_sorted_indexes = vec![root];
        topological_dfs(root, &tree, &mut topological_sorted_indexes);
        for &i in topological_sorted_indexes.iter().rev() {
            if tree[i].is_empty() {
                rank[i] = 1;
                continue;
            }
            rank[i] = tree[i].iter().map(|&x| rank[x]).sum::<u64>() + 1;
        }
        let mut ans = (0..n)
            .map(|x| {
                if x == root || x == centroid[1] {
                    0
                } else {
                    2 * rank[x] * edges[&x][&parent_dict[&x]]
                }
            })
            .sum::<u64>();
        ans += (2 * rank[centroid[1]] - 1) * edges[&centroid[0]][&centroid[1]];
        println!("{}", ans);
    }
}