#include "bits/stdc++.h" using namespace std; using ll = long long; bool isGood(const string& s) { for (int i = 1; i < s.size(); ++i) { if (s[i - 1] > s[i]) { return false; } } return true; } int dp(int curr, char last, const vector, int>>& S, vector& memo) { if (curr == S.size()) { return 0; } if (memo[curr] >= 0) { return memo[curr]; } int ret = 0; if (last <= S[curr].first.first) { int take = dp(curr + 1, S[curr].first.second, S, memo) + S[curr].second; int skip = dp(curr + 1, last, S, memo); ret = max(take, skip); } else { ret = dp(curr + 1, last, S, memo); } memo[curr] = ret; return ret; } void Main() { int N; cin >> N; vector, int>> S; for (int i = 0; i < N; ++i) { string s; cin >> s; if (isGood(s)) { S.push_back(make_pair(make_pair(s[0], s[s.size() - 1]), s.size())); } } sort(S.begin(), S.end()); if (S.size() == 0) { cout << 0 << endl; return; } vector memo(S.size() + 10, -1); int ans = dp(0, 'a', S, memo); cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); std::cout << std::fixed << std::setprecision(15); Main(); return 0; }