#include using namespace std; using VS = vector; using LL = long long; using VI = vector; using VVI = vector; using PII = pair; using PLL = pair; using VL = vector; using VVL = vector; #define ALL(a) begin((a)),end((a)) #define RALL(a) (a).rbegin(), (a).rend() #define SZ(a) int((a).size()) #define SORT(c) sort(ALL((c))) #define RSORT(c) sort(RALL((c))) #define UNIQ(c) (c).erase(unique(ALL((c))), end((c))) #define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++) #define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--) #define debug(x) cerr << #x << ": " << x << endl const int INF = 1e9; const LL LINF = 1e16; const LL MOD = 1000000007; const double PI = acos(-1.0); int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 }; int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 }; /* ----- 2018/09/28 Problem: yukicoder 295 / Link: http://yukicoder.me/problems/no/295 ----- */ /* ------問題------ -----問題ここまで----- */ /* -----解説等----- ----解説ここまで---- */ using LLL = __int128; const LLL Max = 1LL << 62; int main() { cin.tie(0); ios_base::sync_with_stdio(false); VL a(26); FOR(i, 0, 26) { cin >> a[i]; } string t; cin >> t; { VI alphanum(26); FOR(i, 0, SZ(t)) { alphanum[t[i] - 'a']++; } FOR(i, 0, 26) { if (alphanum[i] > a[i]) { cout << 0 << endl; return 0; } } } { VVI alpha(26); alpha[t[0] - 'a'].push_back(1); FOR(i, 0, SZ(t) - 1) { if (t[i] == t[i + 1]) { alpha[t[i] - 'a'].back()++; } else { alpha[t[i + 1] - 'a'].push_back(1); } } struct Frac { LLL n, d; Frac(LL n, LL d) :n(n), d(d) {} bool operator < (const Frac& a) const { return n * (a.n - a.d) < (n - d)*a.n; } // 逆だと思うんですよね }; // sectionに対して、section(i)<=a[i]となるようにaを割り振る auto f = [](LLL a, const VI& section) ->LLL { LLL ret = 1; if (!SZ(section))return 1; if (a > 5e6) { if (SZ(section) == 1) { if (section.front() == 1)return a; if (section.front() == 2)return a * (a - 1) / 2; LLL ret = a * (a - 1)*(a - 2) / 6; assert(ret >= Max); return Max; } if (SZ(section) == 2) { if (section.front() == 1 && section.back() == 1) { return (a) / 2 * (a - a / 2); } } return Max; } priority_queue>pq; FOR(i, 0, SZ(section)) { a -= section[i]; pq.push(Frac(section[i] + 1, section[i])); } while (a > 0) { Frac high = pq.top(); pq.pop(); ret = ret * high.n / (high.n - high.d); if (ret >= Max)return Max; high.n++; pq.push(high); a--; } return ret; }; LLL ans = 1; FOR(i, 0, 26) { LLL ret = f(a[i], alpha[i]); if (ret >= Max) { cout << "hel" << endl; return 0; } ans *= ret; if (ans >= Max) { cout << "hel" << endl; return 0; } } cout << (LL)ans << "\n"; } return 0; }