#include #include using namespace std; typedef pair P; typedef pair PP; string s[200005]; PP p[200005]; int main() { int n; cin >> n; for(int i = 0; i < n; i++) cin >> s[i]; for(int i = 0; i < n; i++){ bool f = true; int k = s[i].size(); for(int j = 0; j < k - 1; j++){ if(s[i][j] > s[i][j + 1]) f = false; } if(f) p[i] = PP(P(s[i][k - 1] - 'a', s[i][0] - 'a'), k); else p[i] = PP(P(-1, -1), k); } sort(p, p + n); int dp[30]{0}; for(int i = 0; i < n; i++){ int r = p[i].first.first, l = p[i].first.second, c = p[i].second; if(r == -1) continue; dp[r] = max(dp[r], dp[l] + c); for(int j = 1; j < 26; j++) dp[j] = max(dp[j], dp[j - 1]); } cout << dp[25] << endl; }