Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length n

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 i-th character increases the ambiguity by ai (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.

Input

The first line contains one integer n

(1≤n≤105

) — the length of the statement.

The second line contains one string s

of length n

, consisting of lowercase Latin letters — the statement written by Vasya.

The third line contains n

integers a1,a2,…,an (1≤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, 5

-th character is removed so the result is hhzawde.

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

题意:给定一个字符串,每个字符自带权值,让你删去一些,使得不存在子序列“hard”,问最下的权值是多少。

思路:因为有顺序问题,所以我们记录维护到当前最长的前缀的代价。1对应h,2对应ha,3对应har,4对应hard,然后就不难写出方程了。

(复杂度O(5N),比赛时写了个2进制,复杂度O(16N);傻了

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
#define ll long long
const int maxn=;
int a[maxn];ll dp[maxn][],ans; char c[maxn]; int Laxt[maxn];
int id(char s){
if(s=='h') return ;
if(s=='a') return ;
if(s=='r') return ;
if(s=='d') return ;
return -;
}
void ADD(ll &x,ll y){
if(y==-) return ;
if(x==-) x=y;
else x=min(x,y);
}
int main()
{
int N;
scanf("%d%s",&N,c+); memset(dp,-,sizeof(dp));
rep(i,,N) scanf("%d",&a[i]);
dp[][]=;
rep(i,,N){
int p=id(c[i]);
if(p==-) {
rep(j,,) dp[i][j]=dp[i-][j];
continue;
}
if(dp[i-1][p-1]!=-1) ADD(dp[i][p-],dp[i-][p-]+a[i]);
ADD(dp[i][p],dp[i-][p-]);
rep(j,,){
if(j==p-) continue;
ADD(dp[i][j],dp[i-][j]);
}
}
ans=1LL<<; rep(i,,) if(dp[N][i]!=-) ans=min(ans,dp[N][i]);
printf("%lld\n",ans);
return ;
}

CF1096:D. Easy Problem(DP)的更多相关文章

  1. D. Easy Problem dp(有衔接关系的dp(类似于分类讨论) )

    D. Easy Problem dp(有衔接关系的dp(类似于分类讨论) ) 题意 给出一个串 给出删除每一个字符的代价问使得串里面没有hard的子序列需要付出的最小代价(子序列不连续也行) 思路 要 ...

  2. Codeforces 1096D - Easy Problem - [DP]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...

  3. CF1096D Easy Problem(DP)

    题意:给出一个字符串,去掉第i位的花费为a[i],求使字符串中子串不含hard的最小代价. 题解:这题的思路还是比较套路的,    dp[i][kd]两维,kd=0表示不含d的最小花费,1表示不含rd ...

  4. CF1096D Easy Problem

    题目地址:CF1096D Easy Problem 比赛时高二dalaoLRZ提醒我是状压,然而,我还是没AC (汗 其实是一道很基础的线性dp \(f_{i,j}\) 表示序列第 \(i\) 个字符 ...

  5. HDU 4359——Easy Tree DP?——————【dp+组合计数】

    Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)

    An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  7. HDU 4359 Easy Tree DP?

    Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. UVA-11991 Easy Problem from Rujia Liu?

    Problem E Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for ...

  9. An easy problem

    An easy problem Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

随机推荐

  1. 【Golang 接口自动化07】struct转map的三种方式

    背景 我们在前面介绍过怎么使用net/http发送json或者map数据,那么它能不能直接发送结构体数据呢?我们今天一起来学习结构体struct转map的三种方法,为后续做铺垫. struct转map ...

  2. angular5 路由变化监听

    1.路由监听 //监听路由变化this.router.events .filter(event => event instanceof NavigationEnd) .map(() => ...

  3. 关于C和C++

    最开始学的就是C和C++,但只是学过,根本就不知道怎么使用. 后来接触了Python和Perl才知道怎么将编程应用于实际需求当中,读取文件,存放到数据结构,处理,输出. 但脚本语言有其固有的缺点,不能 ...

  4. WPF 的 数据源属性 和 数据源

    (一)数据源(数据对象)属性 :path 或  path的值(path=VM.Property或M.Property),通常具有通知功能(特例除外). (二)path不能孤立而存在,它一定具有所归属的 ...

  5. 在WPF中添加Windows Form控件(包括 ocx控件)

      首先,需要向项目中的reference添加两个dll,一个是.NET库中的System.Windows.Forms,另外一个是WindowsFormsIntegration,它的位置一般是在C:\ ...

  6. English trip -- VC(情景课) 6 B Events 事件

    xu言: ...  自己选择的路,就算是爬,也要给我爬完.短短人生数载,我能之止于此? Words appointment  预约 meeting  会议 class movie party prog ...

  7. CF808D STL

    D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  8. 访问IIS元数据库失败的解决方法

    这两天在调试一个Asp.net程序时,出现了“访问IIS元数据库失败”的错误信息,最后终于摸索出了解决问题的方法.公布如下: 1.依次点击“开始”-“运行”. 2.在“运行”栏内输入 “C:\WIND ...

  9. spring创建单例bean

    (使用的spring版本是3.2.10) 在xml文件中配置一个普通的bean,默认使用单例,创建该bean的调用栈如下: ClassPathXmlApplicationContext //Class ...

  10. web安全问题分析及处理

    前言 这是我观看了<前端漏洞分析及处理-蔡慧芨>公开课之后的一个总结及简单实践体会.在可能的情况下我会把他们都实际操作一遍,更加深刻地体会前端安全的重要性. web安全问题有哪些 XSS- ...