Mahmoud wrote a message s of length n. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That's because this magical paper doesn't allow character number i in the English alphabet to be written on it in a string of length more than ai. For example, if a1 = 2 he can't write character 'a' on this paper in a string of length 3 or more. String "aa" is allowed while string "aaa" is not.
Mahmoud decided to split the message into some non-empty substrings so that he can write every substring on an independent magical paper and fulfill the condition. The sum of their lengths should be n and they shouldn't overlap. For example, if a1 = 2 and he wants to send string "aaa", he can split it into "a" and "aa" and use 2 magical papers, or into "a", "a" and "a" and use 3 magical papers. He can't split it into "aa" and "aa" because the sum of their lengths is greater than n. He can split the message into single string if it fulfills the conditions.
A substring of string s is a string that consists of some consecutive characters from string s, strings "ab", "abc" and "b" are substrings of string "abc", while strings "acb" and "ac" are not. Any string is a substring of itself.
While Mahmoud was thinking of how to split the message, Ehab told him that there are many ways to split it. After that Mahmoud asked you three questions:
How many ways are there to split the string into substrings such that every substring fulfills the condition of the magical paper, the sum of their lengths is n and they don't overlap? Compute the answer modulo 109 + 7.
What is the maximum length of a substring that can appear in some valid splitting?
What is the minimum number of substrings the message can be spit in?
Two ways are considered different, if the sets of split positions differ. For example, splitting "aa|a" and "a|aa" are considered different splittings of message "aaa".
Input
The first line contains an integer n (1 ≤ n ≤ 103) denoting the length of the message.
The second line contains the message s of length n that consists of lowercase English letters.
The third line contains 26 integers a1, a2, ..., a26 (1 ≤ ax ≤ 103) — the maximum lengths of substring each letter can appear in.
Output
Print three lines.
In the first line print the number of ways to split the message into substrings and fulfill the conditions mentioned in the problem modulo 109 + 7.
In the second line print the length of the longest substring over all the ways.
In the third line print the minimum number of substrings over all the ways.
In the first example the three ways to split the message are:
a|a|b
aa|b
a|ab
The longest substrings are "aa" and "ab" of length 2.
The minimum number of substrings is 2 in "a|ab" or "aa|b".
Notice that "aab" is not a possible splitting because the letter 'a' appears in a substring of length 3, while a1 = 2.
We need to solve three queries for a given string s of length n, and constraints a[1..26] where a_i is max allowed length of a substring that contains character i. Actually the condition: "character number i in the English alphabet can't be written on it in a string of length more than a_i." That is: for any substring that we write on a paper, its length must be ≤ a_c for every character c that appears in that substring? Or is it that we cannot write the character i in a string of length more than a_i? Let's read carefully: "this magical paper doesn't allow character number i in the English alphabet to be written on it in a string of length more than ai." This phrasing is ambiguous. Usually in Codeforces problem (I recall a problem like this, maybe "Split the string" or something). Let me recall: I think it's problem 766C Mahmoud and a Message. Indeed, there is Codeforces problem 766C - Mahmoud and a Message. The description matches. In that problem, the condition is: for any substring (i.e., the string written on a single paper), for each character that appears in that substring, the length of the substring must be <= a[char]. In other words, the substring's length must not exceed the allowed maximum for any character inside it. So it's a constraint on the substring length based on its characters. For each character c, there's a maximum length L_c; any substring that contains c must have length ≤ L_c. But if a substring contains multiple characters, then it must satisfy the condition for each: i.e., length ≤ min(L_c over all characters in the substring). Because if a substring contains a character with a low limit, that limit restricts the length. So effectively, a substring is valid if its length ≤ min_{c in substring} a_c.
Thus, the problem: given string s of length n, and limits a[1..26], compute:
Number of ways to split s into non-empty substrings (contiguous parts) such that each substring is valid (i.e., its length ≤ the minimum a_c among letters present). The splitting is defined by cut positions between characters. Count modulo 1e9+7.
Maximum possible length of a substring that appears in any valid splitting.
Minimum possible number of substrings over all valid splittings.
n ≤ 1000.
We can solve with DP.
Define dp[i] = number of ways to split prefix of length i (first i characters) into valid substrings. i from 0 to n. dp[0] = 1 (empty prefix, one way: no substrings). For i from 1 to n, we consider last substring starting at position j+1 (1-indexed) ending at i, where j from i-1 down to 0, but only as long as substring s[j+1..i] is valid. So dp[i] = sum_{j from i-1 down to 0 while substring s[j+1..i] valid} dp[j]. Complexity O(n^2) if we check validity each time. Since n <= 1000, O(n^2) is fine.
For checking substring validity quickly, we can precompute for each start position, the minimum allowed length? Actually for each start index l (0-indexed) we can compute the maximum length of a valid substring starting at l. Let's define max_len[l] = maximum length such that substring s[l ... l+len-1] is valid. This can be computed by scanning from l, maintain the minimal a_c encountered. As we extend, the min_a decreases or stays same. We can continue until len > min_a. So max_len[l] = largest L such that for all k from l to l+L-1, L ≤ min_{c in s[l..l+L-1]} a_c? Actually condition: substring length ≤ min_{c in that substring} a_c. So for given start l, we can incrementally extend: keep track of current minimal a_c among characters seen so far, call cur_min. When we add a new character at position r, update cur_min = min(cur_min, a[char]), and now substring length = r-l+1. The substring is valid if (r-l+1) ≤ cur_min. So we can find the farthest r such that this holds. But note that cur_min may become smaller than current length as we extend, so we stop when length > cur_min. So max_len[l] = length of longest valid substring starting at l.
We can compute max_len[l] in O(n^2) naive, but with incremental scanning it's O(n^2) total. Actually we can compute for each l by scanning r forward; that's O(n^2) overall, n=1000, it's fine.
Alternatively, we can precompute for each character its limit, and then for each start, we can compute the allowed max length as the minimum over the window of something? But the simple double loop is okay.
Now for dp: we can compute dp[i] by iterating j from i-1 down to 0, and check if substring s[j..i-1] (if using 0-index) length len = i - j is ≤ min_a in that substring. But we can use precomputed max_len to know: for start j, if i - j <= max_len[j], then valid. So dp[i] = sum_{j=0..i-1} dp[j] * [ (i-j) <= max_len[j] ].
We can compute this sum in O(n) per i by scanning j, so overall O(n^2). Good.
Now second query: Maximum possible length of a substring in any valid splitting. This is essentially the maximum length among all valid substrings (i.e., substrings that satisfy condition) because any valid substring can appear in some splitting? Possibly, if a substring is valid, can we include it as a part of some splitting of the whole string? We need to consider that the rest of the string must also be splittable into valid substrings. However the question: "What is the maximum length of a substring that can appear in some valid splitting?" means: over all possible ways to split the entire string into valid substrings, consider any substring that appears as one of the parts; we want the maximum length among those parts. Since the splitting covers the whole string, each part is a contiguous block. So it's the maximum possible length of a valid block that can be used in a complete partition. That is: find a valid partition of the string; among its parts, take the maximum length; we want to maximize that value over all partitions. In other words, what is the longest possible block that we can have as a piece in a valid partition? This might be less than the longest valid substring overall if the longest valid substring cannot be part of any partition because the remaining part cannot be split. But can that happen? For a given valid substring, if we take it as a block, we need the rest (prefix before it and suffix after it) to be splittable. So the longest valid block that can appear in some partition might be restricted. However we can often partition the remaining parts into single characters perhaps? But need to check: single characters are always valid? Since a single character c: length 1 ≤ a_c? It's given that a_x ≥ 1 for all x (since 1 ≤ a_x). So yes, any single character is valid. So any valid substring, if we isolate it as a block and split the rest into individual characters, would that always be possible? Not necessarily: if the rest of the string contains some adjacent characters that together would violate condition? But we are splitting the rest into single characters, each of which is valid individually. So that is always possible because any single character is valid. So for any valid substring T (contiguous segment of s), we can partition the string as: the prefix before T split into single characters, then T as one block, then the suffix after T split into single characters. However need to ensure that the prefix and suffix themselves can be split into single characters. Yes, each character is valid individually. So this partition is always valid. Wait, but careful: The split must cover the whole string, and we must have no overlaps. If we split the prefix into single characters, that's fine. Similarly suffix. So any valid substring can appear as a block in a valid splitting. But is there any nuance? What if the valid substring is not contiguous? Actually it must be contiguous. So yes, any valid substring can be isolated by cutting at its boundaries, and the rest as individual characters. So the maximum length over all valid substrings (i.e., substrings that satisfy condition) is the answer to the second query. Because we can always include it. But is it always possible to split the prefix and suffix into valid substrings? Since any single character is valid (because a_c ≥ 1), yes. So the answer is simply the maximum length among all valid substrings (i.e., all substrings that meet the condition). That seems straightforward.
However, one might consider: what if the valid substring is the entire string? That's fine. So answer is max valid substring length.
But wait: check example: first example, string "aab", limits: a1=2, others 1. Valid substrings: let's list: "a": length1, valid? 'a' limit 2, length1 ≤2, yes. "aa": length2, characters 'a', limit2 => length2 ≤2 valid. "aab": length3, contains 'a' limit2, length3>2 invalid. "ab": length2, contains 'a' limit2 and 'b' limit1? Actually a2 =? given input: 2 3 1 1 1 ... So for b (2nd letter) a2 = 3? Wait in example: first line: aab, third line: "2 3 1 1 1 ..." Actually they gave: "2 3 1 1 1 1 1 1 ..." So a1=2, a2=3, a3=1,... So 'b' is second letter, limit 3. So "ab" includes 'a' limit2, 'b' limit3, min=2, length2 <=2 valid. So longest valid substring is length 2 (both "aa" and "ab"). Output says 2. That matches.
Second example: string "abcdeabcde", limits: first five: 5,5,5,5,4 then 1's. So we can compute longest valid substring. Probably max length is 4. Output says 4. So indeed.
Thus second answer = maxLen over all substrings that satisfy condition.
We can compute by scanning all substrings, or using precomputed max_len per start, and then take max of max_len[l]. Since max_len[l] gives the longest valid substring starting at l. So answer2 = max_{l} max_len[l].
Now third query: minimum number of substrings in any valid splitting. This is like partition the string into as few pieces as possible, each valid. Equivalent to find the minimal k such that we can cover the string with k valid substrings. This is a DP for min pieces: let f[i] = minimal number of substrings to split prefix of length i. f[0] = 0. For i>0, f[i] = min_{j: 0<=j<i, substring s[j..i-1] valid} (f[j] + 1). Compute via DP similarly.
We can compute f simultaneously with dp. Since n small, we can do O(n^2). dp for number of ways, f for min splits.
One nuance: For dp, we need modulo 1e9+7. So compute dp as described.
Now we also need to ensure that the substring validity check is consistent.
We can implement:
Input: n, s (string of length n), array a[26].
We'll work with 0-index.
Precompute limit for each character: limit[c] = a[ c - 'a' ].
We need for each start index i from 0 to n-1, compute max_len[i] = length of longest valid substring starting at i.
We can do:
for i in range(n):
min_allowed = limit[s[i]]
j = i
while j < n:
# current substring s[i..j] length = j-i+1
min_allowed = min(min_allowed, limit[s[j]])
if j - i + 1 > min_allowed:
break
j += 1
max_len[i] = j - i # because j stops when condition fails, length is (j-i) maybe? Let's reason: we start with j=i, check? Let's implement carefully.
We want to find the maximum L such that for all k in [i, i+L-1], L <= min_{c in substring} a_c. We can incremental: L=1 is always valid because 1 <= a_c for each char? Actually 1 <= a_c since a_c >=1, so yes. So we can always have at least length 1. So we can try to extend while valid.
Initialize cur_min = very large? Actually we set cur_min = limit[s[i]] at start. For length 1: 1 <= cur_min? Since cur_min = limit[s[i]] >= 1, true. Then for L=2: need to include s[i+1]; update cur_min = min(cur_min, limit[s[i+1]]); then check if 2 <= cur_min. Continue until fails.
Thus algorithm:
cur_min = limit[s[i]]
len = 1
while i+len < n:
cur_min = min(cur_min, limit[s[i+len]])
len += 1
if len > cur_min: break
After loop, the maximum valid length is len (if we broke before increment? Let's detail with code:
max_len_i = 0
cur_min = 10**9
for j from i to n-1:
cur_min = min(cur_min, limit[s[j]])
if j - i + 1 > cur_min:
break
max_len_i = j - i + 1
Then after loop, max_len_i is correct.
So we can compute.
Alternatively, we can compute len = 0; while i+len < n: char = s[i+len]; cur_min = min(cur_min, limit[char]); if len+1 > cur_min: break; len++; End. Then max_len[i] = len.
Yes.
Now with max_len we can check validity: substring from i to j (inclusive) is valid if (j - i + 1) <= max_len[i].
Now dp:
dp = [0](n+1); dp[0]=1
min_splits = [inf](n+1); min_splits[0]=0
We can also compute max_substring_length overall as ans2 = max of (max_len[i]).
Now for i from 1 to n (i is length of prefix, i.e., dp[i] for first i characters (indices 0..i-1)):
for j in range(i-1, -1, -1): # j is start index? Actually we want last substring = s[j..i-1] where j from 0 to i-1. So length = i-j.
if (i - j) <= max_len[j]:
dp[i] = (dp[i] + dp[j]) % MOD
min_splits[i] = min(min_splits[i], min_splits[j] + 1)
We can compute in O(n^2).
After loop, answers:
ans1 = dp[n] % MOD
ans2 = max(max_len) (over all start positions)
ans3 = min_splits[n] (which should be integer)
Check examples.
Example 1:
n=3, s="aab", limits: a1=2, a2=3, a3=1,...
Compute max_len:
i=0 (s[0]='a', limit=2):
cur_min=2.
j=0: len=1 <=2, ok.
j=1: s[1]='a', cur_min=min(2,2)=2, len=2 <=2 ok.
j=2: s[2]='b', cur_min=min(2,3)=2, len=3 >2 break. So max_len[0]=2.
i=1 (s[1]='a', limit=2):
cur_min=2.
j=1: len=1 ok.
j=2: s[2]='b', cur_min=min(2,3)=2, len=2 <=2 ok. So max_len[1]=2? Wait substring from index1 to 2 is "ab", length2, valid. So yes max_len[1]=2.
i=2 (s[2]='b', limit=3):
cur_min=3, len=1 ok.
j=3 out-of-range, so max_len[2]=1.
So max_len = [2,2,1]; ans2=2.
i=2:
j=1: length1 <= max_len[1]=2 -> valid. dp[2] += dp[1]=1. min[2] = min(inf, min[1]+1=2) => 2.
j=0: length2 <= max_len[0]=2 -> valid. dp[2] += dp[0]=1 => dp[2]=2. min[2] = min(2, min[0]+1=1) => 1? Wait min[0]=0, plus 1 =1, so min[2] becomes 1. But is that valid? That would be taking the substring from 0 to1 ("aa") as one piece, thus only 1 substring? Actually that partition uses 1 substring of length 2 covering the whole prefix of length 2. So number of substrings =1. So min[2]=1. So far ok.
Edge cases: All a_i=1 => only substrings of length 1 are valid. Then number of ways = 1 (must split into all single chars), max length=1, min splits=n. That matches.
If string length n=1, then dp[1] = dp[0] if length1 <= max_len[0] which is always true, so dp[1]=1, ans2 = max_len[0] =? That will be at least 1, but could be more? For n=1, max_len[0] is at most 1 because it can't extend beyond. So ans2=1, ans3=1.
Now need to compute modulo 1e9+7.
Complexity O(n^2) time, O(n) space (plus maybe O(n) for max_len). n=1000, fine.
Thus we can implement.
Now note: The problem statement says: "character number i in the English alphabet to be written on it in a string of length more than ai." Could also be interpreted as: The character i cannot appear in a substring longer than ai, but if the substring contains other characters, the condition might be different? Actually typical interpretation: The magical paper doesn't allow character i to be written on it in a string of length more than a_i. That means if you try to write a string on that paper, for each character i that appears, the length of the string must be ≤ a_i. So it's the same as described. So correct.
Double-check: Example input 2: "abcdeabcde", a: 5 5 5 5 4 1 ... So characters a,b,c,d,e limits are 5,5,5,5,4 respectively. So substrings of length up to min of those letters present. So "e" appears, limit 4. So any substring containing e must be length ≤4. So max length is 4 because somewhere e restricts. Let's verify "abcde" includes e, min limit = min(5,5,5,5,4)=4, length 5 >4 invalid. So longest valid substring might be "abcd" length4 (a,b,c,d all have limit5 so min=5, length4 ok). Also "bcde"? includes e, min=4, length4 ok. So max length=4. Output 4 matches. So yes.
Now for counting: we must consider that splits cannot overlap, etc. DP covers.
Thus solution is ready.
Now we can write code in Python.
But note: Constraints n up to 1000, but could be up to 10^3. Python O(n^2) fine.
Implementation details:
Read n (int), read s (strip), read list of 26 ints.
Map each char to its limit: use ord(c)-97.
Compute max_len: array of length n.
for i in range(n):
cur_min = 10**9 # or simply set to limit[s[i]] first.
length = 0
for j in range(i, n):
cur_min = min(cur_min, limits[ord(s[j])-97])
if (j - i + 1) > cur_min:
break
length = j - i + 1
max_len[i] = length
Alternatively, we can store the maximum length directly.
for i in range(1, n+1):
for j in range(0, i):
# substring s[j:i] (j is start index, i is end exclusive)
substr_len = i - j
if substr_len <= max_len[j]:
dp[i] = (dp[i] + dp[j]) % MOD
if min_splits[j] + 1 < min_splits[i]:
min_splits[i] = min_splits[j] + 1
After loop:
ans1 = dp[n] % MOD
ans2 = max(max_len) # maximum length over all i
ans3 = min_splits[n]
Print each on separate line.
Check for overflow? Not.
One nuance: In the DP we iterate j from 0 to i-1. But for i up to n, that's fine.
But we must ensure that max_len[j] is defined for j in [0,n-1]. For i=n, j can be n-1, that's fine.
Now we should test with examples.
Example 1 we already did.
Example 2: Let's manually test? I can run mental but better trust.
Potential pitfalls: The minimum number of substrings DP: we initialize min_splits[0]=0 and others INF. In the loop we update using min_splits[j] + 1. Since dp ensures there is at least one way, min_splits[n] will be set.
Now answer 2: maximum length of a substring that can appear in some valid splitting. As argued, it's the maximum possible valid substring length. But we need to ensure that we can always isolate that substring in a valid splitting. However, what if the longest valid substring is, say, length L, but it contains characters that have limits L, but splitting the rest into single characters might cause the substring to be adjacent to a single character that maybe is invalid? No, because the substring itself is isolated as a block, and other blocks are single characters, which are valid. So indeed it works. But consider: if we take the substring and split the rest into single characters, we have a valid partition. But wait: could there be any issue if the substring boundaries cause the substring to not be a valid block because the whole string must be contiguous and we cannot have overlapping? No. So yes.
But is it possible that the longest valid substring cannot be used because the rest cannot be split into valid substrings? Suppose we have a situation where the substring itself is valid, but the prefix or suffix contains a character that forces a longer substring? But we are splitting them into single characters, each of which is valid. So there is no restriction that forces the split to be contiguous across the substring; we can always cut at the boundaries. So yes, it's possible.
Thus ans2 = max over all valid substrings length.
Alternatively, if we wanted to be absolutely sure: The DP for maximum length of a part in a valid partition could be computed by DP as well: we could compute for each partition the max part length and then take max over partitions. But given reasoning, it's just max valid substring length.
But one edge: Suppose the string length is n=2, s="ab", limits: a:1, b:1. Then valid substrings: "a" length1, "b" length1, "ab" length2? For "ab", contains a and b, min limit = min(1,1)=1, length2>1 invalid. So max length=1. Splitting into two single characters gives parts length1. So ans2=1. Fine.
If a=2 for a, b=1. Then "ab" valid? min=1, length2>1 invalid. So max valid substring length=1. Splitting options: only split into "a","b". So ans2=1. That matches.
If a=2, b=2. Then "ab" valid? min=2, length2<=2 valid. So max length=2. Partition "ab" as one piece, that's valid. So ans2=2.
Thus fine.
Now complexity fine.
We should consider mod operations for dp. dp can be large, but modulo.
Now we implement.
Edge case: n up to 1000 but a_i up to 1000. So max_len can be up to n.
Now code.
But I recall the original problem (CF 766C) also had a third query: "minimum number of substrings". Yes.
Now just in case: The problem statement might be ambiguous about the condition: "character number i in the English alphabet to be written on it in a string of length more than ai." Could be interpreted as: If a paper has a string written, then for each character i, the total count of that character in the string cannot exceed a_i? Actually "in a string of length more than ai" suggests that the character cannot appear in a string whose length exceeds a_i. So it's about the length of the string, not count of that character. So it's as we thought.
Let's verify with example: a1=2. He can't write character 'a' on this paper in a string of length 3 or more. So if the string is "aab", it has 'a' characters, length 3, so it's not allowed. So that's correct.
Thus solution stands.
Now implement.
But wait: When checking substring validity, we used condition that length ≤ min a_c. However, there is nuance: "character number i ... to be written on it in a string of length more than ai" might also mean that if the string length exceeds a_i, then you cannot write character i at all on that paper. But if the string length is less than or equal to a_i, you can write any characters? Actually, the phrasing could be interpreted as: For each character i, there is a maximum length of the string such that if the string's length exceeds that, then that particular character is not allowed to appear. So if the string is within that length, that character can appear. So effectively, to allow the whole string, for all characters i present, we need length ≤ a_i. So same.
Thus correct.
Now code:
def solve():
import sys
MOD = 10**9+7
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
n = int(next(it))
s = next(it)
a = [int(next(it)) for _ in range(26)]
limits = a
code
# Precompute max_len
max_len = [0]*n
for i in range(n):
cur_min = 10**9
# Actually we can set cur_min = limits[ord(s[i])-97] and then len=1 initially.
# We'll use loop.
length = 0
for j in range(i, n):
cur_min = min(cur_min, limits[ord(s[j])-97])
if (j - i + 1) > cur_min:
break
length = j - i + 1
max_len[i] = length
dp = [0]*(n+1)
dp[0] = 1
min_splits = [float('inf')]*(n+1)
min_splits[0] = 0
for i in range(1, n+1):
for j in range(0, i):
if (i - j) <= max_len[j]:
dp[i] = (dp[i] + dp[j]) % MOD
if min_splits[j] + 1 < min_splits[i]:
min_splits[i] = min_splits[j] + 1
ans1 = dp[n] % MOD
ans2 = max(max_len)
ans3 = min_splits[n]
# print outputs
print(ans1)
print(ans2)
print(ans3)
Test with sample input.
We'll test quickly by mental or run offline? I'm confident.
One more nuance: For the dp counting, we iterate j from 0 to i-1. This includes j such that dp[j] may be 0 (if prefix j cannot be split). But that's fine; we add zero.
Now, what about the min_splits initialization: if no split possible, min_splits[n] would stay INF, but given constraints, there is always at least one split (single characters) so it will be set. But if some characters had a_i = 0? But a_i >=1, so single characters always valid, so at least one split: each char alone. So min_splits[n] will be at most n. So it's fine.
Now confirm sample 2 manually? Let's compute quickly with small script in mind.
We'll compute for each start i, find longest L such that for each char in substring, L <= min limit. Since the smallest limit among the letters in that substring is the limiting factor. The worst-case is when we include 'e' with limit4. So any substring containing e can be at most length 4. Substrings that don't contain e can be longer, up to maybe 5? Let's check: characters a,b,c,d have limits 5, so min is 5, so substring of length 5 without e is allowed. However, the string is abcdeabcde, so the substring "abcd" (len4) works, "abcda"? That would include 'a' again but also includes 'a'? Actually "abcda" includes a,b,c,d,a: no e? Wait 'e' not present. So "abcda" would be positions 0-4? Actually s[0]=a,1=b,2=c,3=d,4=e, so 0-4 includes e. So the first five characters include e. So "abcd" (0-3) length4, valid. "bcde" includes e, length4. "cde"? includes e, length3 valid? Actually min limit for c,d,e is min(5,5,4)=4? Wait e is 4, so length3 <=4 valid. But the maximum length is 4. Could there be length5 without e? Possibly positions 5-9: s[5]=a,6=b,7=c,8=d,9=e. So substring 5-8 (a,b,c,d) length4, valid. 5-9 includes e, length5 invalid. 0-? what about 1-5? positions 1 to 5: b,c,d,e,a includes e, length5 invalid. 2-6: c,d,e,a,b includes e, length5 invalid. 3-7: d,e,a,b,c includes e, length5 invalid. 4-8: e,a,b,c,d includes e, length5 invalid. So maximum valid substring length appears to be 4. So ans2=4.
Now dp counting: There are 401 ways. Hard to verify manually, but trust.
Now our DP should produce that.
But there is a nuance: In the DP for counting, we used condition based on max_len[j] which gives the maximum length of a valid substring starting at j. That is correct. However, note that for a given start j, there may be valid substrings of various lengths up to max_len[j]. The DP by summing over j effectively counts all ways: for each prefix i, we consider last substring starting at j, length i-j. That substring is valid if its length is ≤ max_len[j]. Since max_len[j] is the maximum valid length, any shorter length is automatically valid? Actually, does a shorter length automatically satisfy the condition? Suppose we have a start j, and max_len[j] = L meaning the substring of length L is valid. For any length ℓ < L, is the substring of length ℓ necessarily valid? It depends. The condition for ℓ is: ℓ ≤ min_{c in s[j..j+ℓ-1]} a_c. Since the set of characters in the shorter substring is a subset of the set in the longer substring? Actually not exactly; the longer substring includes additional characters. The min over a_c for the shorter substring might be larger than the min for the longer substring, because the longer substring may include a character with a lower limit. So it's possible that the shorter substring is valid even if the longer one is not, but here we know that the longer one (length L) is valid, so for the longer substring we have L ≤ min_{c in longer} a_c. For any ℓ < L, we have ℓ < L ≤ min_{c in longer} a_c. However, the min over the shorter substring might be larger or equal to min over the longer substring? Since shorter substring's character set is a subset, the min over that subset is ≥ the min over the superset. Because taking min over a smaller set gives a value that is at least the min over the larger set (since you are ignoring possibly smaller elements). So min_short >= min_long. Thus ℓ ≤ L ≤ min_long ≤ min_short? Actually careful: min_short = min_{c in short} a_c, min_long = min_{c in long} a_c. Because long includes short plus extra characters, so min_long = min(min_short, min_extra). So min_long ≤ min_short. So we have L ≤ min_long. Then for ℓ < L, we have ℓ < L ≤ min_long ≤ min_short? Wait inequality: L ≤ min_long, and min_long ≤ min_short. So ℓ < L ≤ min_long ≤ min_short, thus ℓ < min_short? Actually ℓ < L ≤ min_long, but min_long could be less than min_short? Since min_long = min(min_short, min_extra) ≤ min_short. So from ℓ < L ≤ min_long, we have ℓ < min_long. But we need ℓ ≤ min_short. Since min_short ≥ min_long, ℓ < min_long ≤ min_short, so ℓ < min_short. So ℓ ≤ min_short? ℓ < min_short implies ℓ ≤ min_short because ℓ integer? Actually if min_short = 3 and ℓ = 2, then ℓ < min_short, but condition is ℓ ≤ min_short, which holds. However if ℓ = min_long = 2 and min_short = 3, then ℓ ≤ min_short holds. So indeed if ℓ < L and L ≤ min_long, then ℓ ≤ min_short? Let's check a case where longer substring is valid but a shorter substring might not be? Could that happen? Suppose we have characters with limits: 'a' limit 3, 'b' limit 1. String: "ab". Substring length2: includes both, min = min(3,1)=1, length2>1 invalid. So max_len for start 0 would be 1? Actually length1 "a" valid (limit3 >=1). So L=1. So not an example.
Need a scenario where longer substring is valid, but a shorter substring (contained within) is invalid. Is that possible? For shorter substring to be invalid, its length > min_short. Since min_short ≥ min_long? Actually min_short ≥ min_long. If longer substring is valid: L ≤ min_long. For a shorter substring of length ℓ < L, we have ℓ < L ≤ min_long ≤ min_short. So ℓ ≤ min_short always. So shorter substring is valid. So indeed if a substring of length L is valid, all its prefixes (starting at same start) of length less than L are also valid. Because the min limit over the smaller set is at least as large, and length is smaller. So validity is monotonic in length for a fixed start: if length ℓ is valid, then any length less than ℓ is also valid. Conversely, if a length is invalid, then all larger lengths are invalid. So the condition (i-j) <= max_len[j] exactly captures all valid substrings ending at i. So DP is correct.
Thus we are good.
Now we have to output three lines: number of ways modulo, maximum length, minimum number of substrings.
Let's write final answer.
But wait, there is also a possibility to compute ans2 by DP for maximum length in any partition? But we already argued it's just max valid substring length. However, just to be thorough, could there be a valid partition that includes a substring longer than any valid substring? No, because each substring must be valid, so its length is limited by its own characters. So maximum part length cannot exceed the maximum possible valid substring length. So ans2 = that maximum.
Now check examples: matches.
Now also there might be a nuance: The maximum length of a substring that can appear in some valid splitting - maybe they ask for the longest substring that appears as a part in any splitting, not necessarily the longest possible valid substring if we can use it. But as argued, any valid substring can appear. So answer is same.
Thus solution is done.
Now potential pitfalls: For n up to 1000, Python loops of O(n^2) should be fine (1e6 operations). But we have inner loop for each i, and for each i we loop j from 0 to i-1, which is about n^2/2 = 500k. Plus precomputation O(n^2) maybe also about 500k. Total about 1e6, well within time.
But note: precomputation uses for each i, j from i to n-1; that's also n^2/2 roughly. So total operations maybe 1e6, fine.
Now just implement.
But we could also compute max_len with a more efficient method, but not needed.
Now edge: Input reading: sometimes there might be extra spaces or newlines. Use sys.stdin.read().
Now I'll produce final answer.
But wait: The problem statement says "character number i in the English alphabet to be written on it in a string of length more than ai." Could also be interpreted as: If a paper contains character i, then the length of the string cannot exceed a_i. That's what we used.
Now also note: The split positions are between characters; the substrings are non-empty and cover the whole string. That's what we modeled.
Thus solution is correct.
Now I will write the final answer with proper code.
Let me write in the answer:
python
import sys
def solve():
MOD = 10**9+7
data = sys.stdin.read().split()
if not data:
return
n = int(data[0])
s = data[1]
a = list(map(int, data[2:2+26]))
limits = a
max_len = [0]*n
for i in range(n):
cur_min = 10**9
# actually we can start with limit of first char
for j in range(i, n):
cur_min = min(cur_min, limits[ord(s[j]) - 97])
if j - i + 1 > cur_min:
break
max_len[i] = j - i + 1
dp = [0]*(n+1)
dp[0] = 1
min_parts = [10**9]*(n+1)
min_parts[0] = 0
for i in range(1, n+1):
for j in range(0, i):
if i - j <= max_len[j]:
dp[i] = (dp[i] + dp[j]) % MOD
if min_parts[j] + 1 < min_parts[i]:
min_parts[i] = min_parts[j] + 1
ans1 = dp[n] % MOD
ans2 = max(max_len)
ans3 = min_parts[n]
print(ans1)
print(ans2)
print(ans3)
if __name__ == "__main__":
solve()
That should work.
Now I'll produce final answer with explanation. We need to solve three queries for a string s of length n and limits a[1..26] where a[c] is the maximum allowed length of a substring that contains character c. A substring is valid if its length does not exceed the minimum a among all characters present in it. We need to count the number of ways to split the whole string into valid substrings, find the maximum length of a substring that can appear in any valid splitting, and find the minimum possible number of substrings in a valid splitting.
The solution uses dynamic programming:
Precomputation
For each starting index i (0‑based), compute max_len[i] – the length of the longest valid substring that starts at i.
This is done by scanning forward from i, keeping the current minimum allowed value among the characters seen. The substring can be extended while its current length ≤ that minimum.
Counting ways and minimal number of pieces
Let dp[i] be the number of valid splittings of the prefix of length i (i from 0 to n; dp[0]=1).
Let minp[i] be the minimum number of substrings needed for that prefix (minp[0]=0).
For each i we consider all possible last substrings s[j..i-1] (with 0 ≤ j < i). This substring is valid exactly when its length i-j is ≤ max_len[j].
Then dp[i] = Σ dp[j] (mod 1e9+7) over all such j, minp[i] = min(minp[i], minp[j] + 1).
Both tables are filled in O(n²) time.
Maximum substring length
Because any valid substring can be isolated in a splitting (the rest of the string can be split into single characters, each always valid), the answer is simply the maximum max_len[i] over all starts.
The algorithm runs in O(n²) time and O(n) memory, which is fine for n ≤ 1000.