#include using namespace std; using std::cout; using std::cin; using std::endl; using ll=long long; const ll mod=1e9+7; #define rep(i,a) for (ll i=0;i std::vector Z_algo(std::vector &vec){ int n=vec.size(); int ind=1,j=0,k; std::vector ans(n,0); ans[0]=n; while(ind Z_algo(std::string &vec){ int n=vec.size(); int ind=1,j=0,k; std::vector ans(n,0); ans[0]=n; while(ind>N; vector A(N); ll sum=0; rep(i,N) cin>>A[i],sum+=A[i]; string S; cin>>S; assert(S.size()==N); assert(sum<=1e8); string T=""; rep(i,N) rep(j,A[i]) T+=S[i]; auto p=Z_algo(T); ll ans=0; for(auto x:p) ans+=x; cout<