#include using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (int i = m; i < n; ++i) #define FORR(i, m, n) for (int i = m; i >= n; --i) #define ALL(v) (v).begin(),(v).end() templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b> n; vector> d(26,vector(26)); vector c(26); REP(i,n){ string s;cin >> s; string u=s; sort(ALL(u)); int t=s.size(); if(u==s){ if(s[0]==s[t-1]) c[i]+=t; else{ int a=s[0]-'a',b=s[t-1]-'a'; chmax(d[a][b],t); } } } vector dp(26); REP(i,26){ dp[i]+=c[i]; FOR(j,i+1,26) chmax(dp[j],dp[i]+d[i][j]); } cout << dp[25] << endl; }