#include<cstdio> #include<algorithm> usingnamespace std; typedeflonglong LL; constint N = 2e5 + 10; char s[N]; int sa[N], rnk[N], ht[N], x[N], y[N], n, m, c[N]; voidSA() { for (int i = 1; i <= n; i++) c[x[i] = s[i]]++; for (int i = 1; i <= m; i++) c[i] += c[i - 1]; for (int i = n; i; i--) sa[c[x[i]]--] = i; for (int k = 1; k <= n; k <<= 1) { int cnt = 0; for (int i = n - k + 1; i <= n; i++) y[++cnt] = i; for (int i = 1; i <= n; i++) if (sa[i] > k) y[++cnt] = sa[i] - k; for (int i = 1; i <= m; i++) c[i] = 0; for (int i = 1; i <= n; i++) c[x[i]]++; for (int i = 2; i <= m; i++) c[i] += c[i - 1]; for (int i = n; i; i--) sa[c[x[y[i]]]--] = y[i]; for (int i = 1; i <= n; i++) y[i] = x[i]; for (int i = 1; i <= n; i++) x[i] = 0; x[sa[1]] = 1; cnt = 1; for (int i = 2; i <= n; i++) if (y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]) x[sa[i]] = cnt; else x[sa[i]] = ++cnt; if (cnt == n) return; m = cnt; } } voidHeight() { for (int i = 1; i <= n; i++) rnk[sa[i]] = i; for (int i = 1; i <= n; i++) { if (rnk[i] == 1) continue; int j = sa[rnk[i] - 1], k = max(0, ht[rnk[i - 1]] - 1); while (i + k <= n && j + k <= n && s[i + k] == s[j + k]) k++; ht[rnk[i]] = k; } } intmain() { scanf("%d%s", &n, s + 1); for (int i = 1; i <= n; i++) s[i] = s[i] - 'a' + 1; m = 26; SA(); Height(); LL res = (LL)n * (n + 1) / 2; for (int i = 2; i <= n; i++) res -= ht[i]; printf("%lld", res); return0; }