const INF: usize = 1usize << 60; fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let s = (0..n).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: usize = temp.trim().parse().unwrap(); temp }) .collect::>(); let mut m = String::new(); std::io::stdin().read_line(&mut m).ok(); let m: usize = m.trim().parse().unwrap(); let mut paths = vec![vec![INF; n]; n]; for _ in 0..m { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let a = temp[0]; let b = temp[1]; let c = temp[2]; paths[a][b] = c; paths[b][a] = c; } for i in 0..n { paths[i][i] = 0; } for k in 0..n { for i in 0..n { for j in 0..n { paths[i][j] = paths[i][j].min(paths[i][k] + paths[k][j]); } } } let mut result = INF; for i in 1..n-1 { for j in i+1..n-1 { result = result.min(s[i]+s[j]+paths[0][i]+paths[i][j]+paths[j][n-1]); result = result.min(s[i]+s[j]+paths[0][j]+paths[i][j]+paths[i][n-1]); } } println!("{}", result); }