题目:http://codeforces.com/contest/1096/problem/D
题意:给你一个字符串,每个字符有权值,问要删除的最小权值使得这个字符串中没有hard
思路:可以看作一个动态规划问题,dp代表无法构成前缀字符的代价,dp[1]对应h前缀字符的代价,dp[2]对应ha前缀字符的代价,dp[3]对应har的代价,dp[4]对应hard代价。
转移方程就相当于我们每次min(不保留该字母,不保留前缀)答案就是最后不保留hard 的值
#include<bits/stdc++.h>
#define fi first
#define se second
#define FOR(a) for(int i=0;i<a;i++)
#define sc(a) scanf("%d",&a)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, int> LP;
const ll inf = 1e17 + 10;
const int N = 1e6 + 10;
const ll mod = 998244353;
map<string, int>ml;
ll b[N], vis[N], po[N], num[N], t, n, m, x, y, k, f[N],a[N];
ll ex, ey, cnt, ans, sum, flag;
ll dist[N];
ll dp[N];
string s ;
vector<int> v[N];
map<int, int> mp;
int main()
{
ios::sync_with_stdio ( false );
cin.tie ( 0 );
cin>>n;
cin>>s;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;s[i];i++)
{
if(s[i]=='h') num[1]+=a[i];
if(s[i]=='a') num[2]=min(num[1],num[2]+a[i]);
if(s[i]=='r') num[3]=min(num[2],num[3]+a[i]);
if(s[i]=='d') num[4]=min(num[3],num[4]+a[i]);
}
cout<<num[4];
}