use std::io::*; fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); const BASE: u8 = 'a' as u8; let mut map: Vec> = vec![Vec::new(); 26]; for _ in 0..n { let a: Vec = itr.next().unwrap().bytes().map(|b| b - BASE).collect(); let m = a.len(); let mut ok = true; for j in 0..m - 1 { if a[j] > a[j + 1] { ok = false; break; } } if ok { map[a[0] as usize].push((a[m - 1] as usize, m)); } } for i in 0..26 { map[i].sort(); } let mut dp: Vec = vec![0; 27]; for from in 0..26 { for &(to, len) in map[from].iter() { dp[to] = std::cmp::max(dp[to], dp[from] + len); } dp[from + 1] = std::cmp::max(dp[from + 1], dp[from]); } println!("{}", dp[26]); }