#include #include #include #include #include #include #include #include #include #include #include #include #include #define getchar getchar_unlocked #define putchar putchar_unlocked #define _rep(_1, _2, _3, _4, name, ...) name #define rep2(i, n) rep3(i, 0, n) #define rep3(i, a, b) rep4(i, a, b, 1) #define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c)) #define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__) using namespace std; using i64 = long long; using u32 = unsigned; using u64 = unsigned long long; using f80 = long double; using i128 = __int128_t; void solve() { const i64 lim = i64(1) << 62; i64 counts[26]; static char str[1000010]; while (~scanf("%lld", &counts[0])) { rep(i, 1, 26) scanf("%lld", &counts[i]); scanf("%s", str); const int slen = strlen(str); rep(i, slen) str[i] -= 'a'; vector lens[26]; int freqs[26] = {}; { int i = 0; for (int j; i < slen; i = j) { int c = str[i]; j = i + 1; while (j < slen && str[j] == c) ++j; lens[c].push_back(j - i); freqs[c] += j - i; } } struct Fraction { Fraction() {} Fraction(int n, int d) : n(n), d(d) {} bool operator < (const Fraction& rhs) const { return i64(n) * rhs.d < i64(d) * rhs.n; } int n, d; }; i128 ans = 1; rep(i, 26) { if (freqs[i] == 0) continue; if (freqs[i] > counts[i]) { ans = 0; break; } i64 rest = counts[i] - freqs[i]; auto binom = [&] (i64 n, int k) { i128 ret = 1; rep(i, k) { ret = ret * (n - i) / (i + 1); if (ret >= lim) return i128(lim); } return ret; }; sort(lens[i].begin(), lens[i].end()); bool brute = false; i128 prod = 1; if (lens[i].size() <= 2) { if (lens[i].size() == 1) { prod = binom(counts[i], lens[i][0]); } else { if (lens[i][1] == 1) { prod = i128(counts[i] / 2) * (counts[i] - counts[i] / 2); } else brute = true; } } else brute = true; if (brute) { using P = pair; priority_queue< pair > que; rep(id, lens[i].size()) { que.push({{lens[i][id] + 1, 1}, {id, 1}}); } for (; rest; --rest) { auto d = que.top(); que.pop(); auto f = d.first; int id, j; tie(id, j) = d.second; prod = prod * f.n / f.d; if (prod >= lim) break; auto nf = Fraction(lens[i][id] + j + 1, j + 1); que.push({nf, {id, j + 1}}); } } if (prod >= lim || (ans *= prod) >= lim) { ans = lim; break; } } if (ans >= lim) puts("hel"); else printf("%lld\n", i64(ans)); } } int main() { auto beg = clock(); solve(); auto end = clock(); fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC); }