#include #include #include #include using namespace std; bool good(string &s) { char mx = 'a'; for (char& c : s) { if (mx > c) return false; mx = c; } return true; } int main(void) { int n; cin >> n; vector ok; for (int i = 0; i < n; ++i) { string s; cin >> s; if (good(s)) ok.push_back(s); } int m = ok.size(); long long dp[26]; memset(dp, 0, sizeof(dp)); sort(ok.begin(), ok.end(), [](string &x, string &y){ if (x.back() != y.back()) return x.back() < y.back(); return x < y; }); for (int i = 0; i < m; ++i) { int from = dp[ok[i].front() - 'a']; int to = ok[i].back() - 'a'; for (int j = to; j < 26; ++j) { dp[j] = max(dp[j], from + (long long)ok[i].size()); } } long long ans = 0; for (int i = 0; i < 26; ++i) { ans = max(ans, dp[i]); } cout << ans << endl; }