字符串dp(只考虑删除)

D. Easy Problem

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length nn 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 00, and removing ii-th character increases the ambiguity by aiai (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 44 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.

Input

The first line contains one integer nn (1≤n≤1051≤n≤105) — the length of the statement.

The second line contains one string ss of length nn, consisting of lowercase Latin letters — the statement written by Vasya.

The third line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤9982443531≤ai≤998244353).

Output

Print minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.

Examples

input

Copy

6
hhardh
3 2 9 11 7 1

output

Copy

5

input

Copy

8
hhzarwde
3 2 6 9 4 8 7 1

output

Copy

4

input

Copy

6
hhaarr
1 2 3 4 5 6

output

Copy

0

Note

In the first example, first two characters are removed so the result is ardh.

In the second example, 55-th character is removed so the result is hhzawde.

In the third example there's no need to remove anything.

//然后考虑一下,当遇到a的时候,我们就考虑构成h的最小花费,当遇到har
//的时候,我们就考虑构成ha的最小花费,当遇到hard的时候,
//我们就考虑构成hard的最小花费就可以了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#include<cctype>
using namespace std;

typedef long long ll;
const int Maxn=210000;

int n,a[Maxn];
ll f[Maxn][4];
char s[Maxn];

int main() {
    scanf("%d",&n);
    scanf("%s",s);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    
    memset(f,0x3f,sizeof(f));
    f[0][0]=0;
    
    for(int i=1;i<=n;i++) {
        switch(s[i-1]) {
            case 'h' : {
                f[i][0]=f[i-1][0]+a[i];
                f[i][1]=min(f[i-1][1],f[i-1][0]);
                f[i][2]=f[i-1][2];
                f[i][3]=f[i-1][3];
                break;
            }
            case 'a' : {
                f[i][0]=f[i-1][0];
                f[i][1]=f[i-1][1]+a[i];
                f[i][2]=min(f[i-1][2],f[i-1][1]);
                f[i][3]=f[i-1][3];
                break;
            }
            case 'r' : {
                f[i][0]=f[i-1][0];
                f[i][1]=f[i-1][1];
                f[i][2]=f[i-1][2]+a[i];
                f[i][3]=min(f[i-1][3],f[i-1][2]);
                break;
            }
            case 'd' : {
                f[i][0]=f[i-1][0];
                f[i][1]=f[i-1][1];
                f[i][2]=f[i-1][2];
                f[i][3]=f[i-1][3]+a[i];
                break;
            }
            default : {
                f[i][0]=f[i-1][0];
                f[i][1]=f[i-1][1];
                f[i][2]=f[i-1][2];
                f[i][3]=f[i-1][3];
                break;
            }
        }
    }
    printf("%I64d\n",min(f[n][0],min(f[n][1],min(f[n][2],f[n][3]))));
    return 0;
}

/*
对于这种让其无法构成一种字符串的dp , 
状态是构成其前缀字符串,
转移便是 对于无法构成前i个前缀的最小花费 
首先可以考虑让他无法构成前i-1个字符的最小花费? 
然后便是考虑将这个位置的字符删除 用滚动数组 dp[i] 无法构成前i个字符的最小花费
*/
#include<bits/stdc++.h>
using namespace std;
#define Sheryang main
const int maxn=1e6+7;
typedef long long ll;
const int mod=998244353;
 
ll a[maxn];
char s[maxn];
ll dp[5];
int Sheryang()
{
	int n;
    cin>>n; 
    scanf("%s",s+1);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
 
    for(int i=1;s[i];i++){
        if(s[i]=='h'){
            dp[1]=dp[1]+a[i];
        }else if(s[i]=='a'){
            dp[2]=min(dp[1],dp[2]+a[i]);
        }else if(s[i]=='r'){
            dp[3]=min(dp[2],dp[3]+a[i]);
        }else if(s[i]=='d'){
            dp[4]=min(dp[3],dp[4]+a[i]);
        }
    }
 
    printf("%lld\n",dp[4]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/87872238