#include #include #include using namespace std; int main(){ int n;cin>>n; vector A; for(int i = 0; n > i; i++){ string s;cin>>s; bool ok = true; for(int j = 1; s.size() > j; j++){ if(s[j-1] > s[j]){ ok = false;break; } } if(ok){ A.push_back(s); } } sort(A.begin(),A.end()); vector ans(26,0); for(int i = 0; A.size() > i; i++){ for(int j = A[i][0]-'a'; 0 <= j; j--){ ans[A[i][A[i].size()-1]-'a'] = max(ans[A[i][A[i].size()-1]-'a'],ans[j]+(int)A[i].size()); } } int ret = 0; for(int i = 0; 26 > i; i++){ ret = max(ans[i],ret); } cout << ret << endl; }