/** * code generated by JHelper * More info: https://github.com/AlexeyDmitriev/JHelper * @author aajisaka */ #include using namespace std; void debug_out() { cerr << endl; } template void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif #define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr) #define rep(i,n) for(int i=0; i<(int)(n); i++) #define all(v) v.begin(), v.end() template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using P = pair; constexpr double PI = 3.14159265358979323846; mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count()); class LCPs { public: void solve(istream& cin, ostream& cout) { SPEED; int n; cin >> n; vector vec(n); ll ans = 0; rep(i, n) { cin >> vec[i]; ans += vec[i].size(); } vector d(n); rep(i, n-1) { int j=0; while(j st; rep(i, n) { if (st.empty() || st.top().second < d[i]) { st.emplace(i, d[i]); } else if (st.top().second == d[i]) { continue; } else { ll pf; while(!st.empty() && st.top().second > d[i]) { auto p = st.top(); st.pop(); ll low = d[i]; if (!st.empty()) chmax(low, st.top().second); ll k = p.second - low; ll r = i - p.first; pf = p.first; debug(k, r); ll now = r*(r+1)*(r+5)/6*k; ans += now; // k, r // (r+1) + r*2 + (r-1)*3 + ... + 2*r // r = 1 -> 2*1 = 2 // r = 2 -> 3*1 + 2*2 = 7 // r = 3 -> 4*1 + 3*2 + 2*3 = 16 // r = 4 -> 5*1 + 4*2 + 3*3 + 2*4 = 30 // r = 5 -> 6*1 + 5*2 + 4*3 + 3*4 + 2*5 = 50 // r = r -> r*(r+1)*(r+5)/6; } if (st.empty() || st.top().second < d[i]) { if (d[i] != 0) st.emplace(pf, d[i]); } } } cout << ans << endl; } }; signed main() { LCPs solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }