Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length ? consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaard can be considered hard statements, while har, hart and drah are easy statements.
Vasya doesn’t want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is 0, and removing ?-th character increases the ambiguity by ?? (the index of each character is considered as it was in the original statement, so, for example, if you delete character r from hard, and then character d, the index of d is still 4 even though you delete it from the string had).
Vasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it!
Recall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.
The first line contains one integer n ( 1 ≤ n ≤ 1 0 5 ) n\ (1≤n≤10^5) n (1≤n≤105) — the length of the statement.
The second line contains one string ? of length ?, consisting of lowercase Latin letters — the statement written by Vasya.
The third line contains ? integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 998244353 ) . a_1,a_2,…,a_n (1≤a_i≤998244353). a1,a2,…,an(1≤ai≤998244353).
Print minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.
6
hhardh
3 2 9 11 7 1
5
8
hhzarwde
3 2 6 9 4 8 7 1
4
6
hhaarr
1 2 3 4 5 6
0
In the first example, first two characters are removed so the result is a r d h ardh ardh.
In the second example, 5 − t h 5-th 5−th character is removed so the result is h h z a w d e hhzawde hhzawde.
In the third example there’s no need to remove anything.
综上所述,转移方程如下:
d p [ i ] [ j ] = { d p [ i − 1 ] [ j ] s [ i ] ! = t [ j − 1 ] m i n ( d p [ i − 1 ] [ j − 1 ] , d p [ i − 1 ] [ j ] + a [ i ] ) e l s e dp[i][j]=\left\{ \begin{array}{rcl} dp[i-1][j] & & {s[i]!=t[j-1]}\\ min(dp[i-1][j-1],dp[i-1][j]+a[i]) & & {else}\\ \end{array} \right. dp[i][j]={dp[i−1][j]min(dp[i−1][j−1],dp[i−1][j]+a[i])s[i]!=t[j−1]else
#include
using namespace std;
typedef long long ll;
const int maxn=100005;
int n,a[maxn];ll dp[100005][10];
char s[100005];
char t[]="hard";
int main()
{
scanf("%d",&n);
scanf("%s",s+1);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(dp,0x3f,sizeof(dp));
for(int i=1;i<=4;i++) dp[0][i]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=4;j++){
if(s[i]!=t[j-1]) dp[i][j]=dp[i-1][j];
else dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+1LL*a[i]);
}
}
printf("%lld\n",dp[n][4]);
}