func main() var n: int :: cui@inputInt() var map: dict :: #dict for(1, n) var s: []char :: cui@input() var cStart: int var cEnd: int var len: int do f(s, &cStart, &cEnd, &len) if(len <> 0) var id: int :: cStart + cEnd * 26 if(cStart = cEnd) do map.add(id, map.get(id, &) + len) else do map.add(id, [map.get(id, &), len].max()) end if end if func f(s: []char, cStart: &int, cEnd: &int, len: &int) for i(1, ^s - 1) if(s[i] < s[i - 1]) do len :: 0 ret end if end for do cStart :: s[0] $ int - 'a' $ int do cEnd :: s[^s - 1] $ int - 'a' $ int do len :: ^s end func end for var dp: []int :: #[26]int for cs(0, 25) for ce(cs, 25) do dp[ce] :: [dp[ce], dp[cs] + map.get(cs + ce * 26, &)].max() end for end for var ans: int :: dp[25] do cui@print("\{ans}\n") end func