use std::cmp::*; //use std::collections::*; use std::io::*; use std::str::FromStr; fn read() -> T { let stdin = stdin(); let stdin = stdin.lock(); let token: String = stdin .bytes() .map(|c| c.expect("failed to read char") as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); token.parse().ok().expect("failed to parse token") } fn main(){ let n:usize = read(); let a:Vec = (0..n).map(|_| read()).collect(); let mut minf:Vec = vec![10000000000;n]; let mut minb:Vec = vec![10000000000;n]; minf[0] = min(minf[0],a[0]); minb[n - 1] = min(minf[n - 1],a[n - 1]); for i in 1..n{ minf[i] = min(minf[i - 1],a[i]); minb[n - 1 - i] = min(minb[n - i], a[n - 1 - i]); } let mut ans:i64 = 1e18 as i64; let mut cnt:i64 = 0; for i in 1..n - 1{ if minf[i - 1] < a[i] && minb[i + 1] < a[i]{ ans = min(ans,a[i]+minb[i + 1]+minf[i - 1]); cnt += 1; } } if cnt == 0 { ans = -1; } println!("{}",ans); }