func main() var n: int :: cui@inputInt() var map: dict :: #dict for loop(1, n) var s: []char :: cui@input() for i(1, ^s - 1) if(s[i] < s[i - 1]) skip loop end if end for var cStart: int :: s[0] $ int - 'a' $ int var cEnd: int :: s[^s - 1] $ int - 'a' $ int var len: int :: ^s 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 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