#include<iostream> #include<vector> #include<algorithm> #include<cstring> #include<queue> #define maxn 100010 #define ll long long usingnamespacestd;
int n, a[maxn]; char s[maxn];
int nxt[maxn]; voidinit_nxt(char *s){ int k = 0, l = strlen(s + 1); nxt[1] = 0; for (int i = 2; i <= l; ++i) { while (k && s[k + 1] != s[i]) k = nxt[k]; if (s[k + 1] == s[i]) ++k; nxt[i] = k; } }
cin >> n >> s + 1; init_nxt(s); priority_queue<node> Q; for (int i = 1; i <= n; ++i) cin >> a[i]; init_fa(n); for (int i = 1; i <= n; ++i) ans += a[i], Q.push({ i, 1, a[i] }); while (!Q.empty()) { int x = Q.top().k; Q.pop(); if (x != find(x)) continue; int u = find(x), v = find(nxt[u]); if (v != u) merge(u, v), Q.push({ v, sz[v], sum[v] }); } cout << ans << "\n"; return0; }