#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 inf = i64(1) << 62; const i128 denom = 2e20; i64 counts[26]; static char str[1000010]; while (~scanf("%lld", &counts[0])) { rep(i, 1, 26) scanf("%lld", &counts[i]); scanf("%s", str); int slen = strlen(str); rep(i, slen) str[i] -= 'a'; vector lens[26]; int freqs[26] = {}; lens[int(str[0])].push_back(1); freqs[int(str[0])]++; rep(i, 1, slen) { if (str[i] == str[i - 1]) lens[int(str[i])].back()++; else lens[int(str[i])].push_back(1); freqs[int(str[i])]++; } i128 ans = 1; rep(i, 26) { if (counts[i] < freqs[i]) { ans = 0; break; } if (freqs[i] == 0 || counts[i] == freqs[i] || ans >= inf ) { continue; } auto binom = [&] (i64 a, int k) { i128 ret = 1; rep(i, k) { ret = ret * (a - k + 1 + i) / (i + 1); if (ret >= inf) return inf; } return i64(ret); }; i128 prod = 1; if (lens[i].size() == 1) { prod = binom(counts[i], lens[i][0]); } else { i64 rest = counts[i] - freqs[i]; if (rest >= 2e9) { prod = inf; } else { i128 ok = denom + 1, ng = slen * denom + 1; bool exact = false; while (ng - ok > 1) { i128 mid = (ok + ng) >> 1; i128 total = 0; for (auto l : lens[i]) total += (l * denom) / (mid - denom); if (total >= rest) { ok = mid; if (total == rest) { exact = true; ng = mid; } } else if (total < rest) { ng = mid; } } for (auto l : lens[i]) { i64 k = (l * denom) / (ng - denom); i64 b = binom(l + k, l); prod = min(i128(inf), prod * b); rest -= k; } assert(rest >= 0); if (!exact && prod < inf) { for (auto l : lens[i]) { i64 k1 = (l * denom) / (ok - denom); i64 k2 = (l * denom) / (ng - denom); if (k1 > k2) { assert(k1 == k2 + 1); prod = prod * (k1 + l) / k1; prod = min(i128(inf), prod); } if (prod >= inf) break; if (--rest == 0) break; } if (prod < inf) assert(rest == 0); } } } if (prod >= inf || (ans *= prod) >= inf) { ans = inf; } } if (ans >= inf) 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); }