#include #include using namespace std; using namespace atcoder; #define ll long long #define ull unsigned long long #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, a, b) for(int i = a; i <= b; i++) #define rrep(i, n) for(int i = n; i >= 0; i--) #define rrep2(i, a, b) for(int i = a; i >= b; i--) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define eb emplace_back #define fi first #define se second #define sz(x) (int)x.size() #define pii pair #define pll pair #define endl '\n' template using pq = priority_queue< T >; template using pqg = priority_queue< T , vector< T >, greater< T >>; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int inf = 1e9 + 1; const ll INF = 1e18 + 1; const int MOD = 1000000007; const double PI = 3.1415926535; bool range(int y, int x, int h, int w){ return (0 <= y && y < h && 0 <= x && x < w); } int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int main(){ cout << fixed << setprecision(15); int n; cin >> n; vector vec; rep(i, n){ string s; cin >> s; string t = s; sort(all(t)); if(s == t) vec.eb(s); } sort(all(vec)); int m = vec.size(); vector dp(m+1, vector(26)); rep(i, m){ int a = vec[i][0] - 'a'; int b = vec[i].back() - 'a'; rep(j, 26){ chmax(dp[i+1][j], dp[i][j]); if(j <= a){ chmax(dp[i+1][b], dp[i][j] + (int)vec[i].size()); } } } int ans = 0; rep(i, 26) chmax(ans, dp[m][i]); cout << ans << endl; return 0; }