#include #include using namespace std; using LL = long long; using P = pair; using Graph = vector>; const int INF = 1 << 29; const long long LINF = 1LL << 60; #define all(x) (x).begin(), (x).end() #define rep(i,n) for(int i = 0; i < (n); ++i) templatevoid chmin(T&a, T b){if(a > b) a = b;} templatevoid chmax(T&a, T b){if(a < b) a = b;} int main(){ LL N; cin >> N; vector> tmp(26, vector(26, 0)); for(LL i = 0; i < N; ++i){ string S, T; cin >> S; T = S; sort(S.begin(), S.end()); if(T!=S) continue; int a = S[0] - 'a', b = S[(int)S.size()-1] - 'a'; if(a==b) tmp[a][b] += (int)S.size(); else tmp[a][b] = max(tmp[a][b], (int)S.size()); } /* for(int i = 0; i < 26; ++i){ for(int j = 0; j < 26; ++j){ cout << tmp[i][j] << " "; } cout << endl; } */ vector dp(26, 0); dp[0] = 0; for(int i = 0; i < 26; ++i){ for(int j = 0; j < 26; ++j){ if(j