Codeforces 1096D

現在有一個字符串,你需要用ai的錢去掉這個字符串的第i個位置的字符。現在要使得該字符串中不包含子序列hard。求最小錢數輸入第一行一個整數n表示字符串的長度(1<=n<=100000)。 第二行一個給定的字符串。 第三行n個整數a1,a2,a3,...,an(1<=ai<=998244353)。輸出輸出一個整數表示答案。樣例

Input
6
hhardh
3 2 9 11 7 1
Output
5
Input
8
hhzarwde
3 2 6 9 4 8 7 1
Output
4
Input
6
hhaarr
1 2 3 4 5 6
Output
0

說明第一個樣例,最開始的兩個字符被刪除了,所以結果是ardh。 第二個樣例,第5個字符被刪除所以結果是hhzawde。 第三個樣例,不需要刪除任何字符。

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 100010
#define INF 10000000000000000LL
using namespace std;
int n,a[maxn];
char s[maxn],st[6]=" hard";
long long dp[maxn][6];
int main(){
    scanf("%d",&n);
    scanf("%s",s+1);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=0;i<=n;i++)
        for(int j=0;j<=5;j++)dp[i][j]=INF;
    dp[0][0]=0;
    for(int i=1;i<=n;i++)
        dp[i][0]=dp[i-1][0]+(s[i]=='h'?a[i]:0);
    for(int i=0;i<n;i++){
        for(int j=0;j<=4;j++){
            if(s[i+1]==st[j+1]){
                dp[i+1][j]=min(dp[i+1][j],dp[i][j]+a[i+1]);
                dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]);
            }
            else dp[i+1][j]=min(dp[i+1][j],dp[i][j]);
        }
    }
    long long ans=min(min(dp[n][0],dp[n][1]),min(dp[n][2],dp[n][3]));
    printf("%lld",ans); 
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章