#P9190. Bessie Substring Deletion

    ID: 22345 Type: Default 1000ms 256MiB

Bessie Substring Deletion

Bessie Substring Deletion

You are given a string \(s\) of length at most \(2\cdot 10^5\) consisting only of lowercase letters a–z. Each character has an associated deletion cost. Your task is to delete zero or more characters so that the remaining string is exactly a concatenation of one or more copies of the word \(\text{bessie}\) (i.e. bessiebessiebessie... etc.).

Formally, let \(s'\) be the string obtained after deleting some characters (possibly none) from \(s\). You must have

[ s' = \underbrace{\text{bessie} , \text{bessie} \cdots \text{bessie}}_{k\text{ times}} ]

for some maximum possible integer (k\ge 0). Among all ways of achieving this maximum (k), you need to choose one with the minimum total deletion cost.

Output two numbers: the maximum number \(k\) of copies of \(\text{bessie}\) that can be obtained, and the minimum total cost required to delete the unwanted characters.

inputFormat

The first line contains a single integer \(n\) (the length of the string).

The second line contains the string \(s\) of length \(n\), consisting only of lowercase English letters.

The third line contains \(n\) space‐separated integers, where the \(i\)th integer represents the deletion cost for the \(i\)th character in \(s\).

outputFormat

Output two space-separated integers on a single line. The first integer is the maximum number \(k\) of contiguous occurrences of the word \(\text{bessie}\) that can be formed, and the second is the minimum total deletion cost required.

sample

6
bessie
1 1 1 1 1 1
1 0