Codefroces: D. Easy Problem

题目链接:http://codeforces.com/contest/1096/problem/D

D. Easy Problem

time limit per test 2 seconds
memory limit per test 256 megabytes

problem Decription

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
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

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.

解题心得:

  1. 题意就是给你一个长度为n的字符串,你可以对字符进行删除,每个字符删除有个代价。要求你对字符删除之后不能出现hard的子序列。
  2. 就是一个dp,因为不能出现hard,所以删除的只有四个字符h、a、r、d。如果从h开始转移就可能导致并不能出现hard但是删除了h、a、r等字符得到的代价是错误的,所以在dp的时候应该从d开始删除,如果可以删除d那么可以从d的状态上转移到r、a、d三个字符。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
typedef long long ll;

ll dp[8][maxn];
int num[maxn], n;

char s[maxn], aim[10] = "hard";

void init() {
    scanf("%d",&n);
    scanf("%s", s+1);
    for(int i=1;i<=n;i++) {
        scanf("%d", &num[i]);
    }

    for(int i=0;i<=3;i++) {
        for(int j=0;j<=n+1;j++) {
            dp[i][j] = 1e18;
        }
    }
}

void DP() {
    for(int j=n;j>=1;j--) {
        if(s[j] == aim[3]) {
            if(dp[3][j+1] == 1e18)
                dp[3][j] = num[j];
            else
                dp[3][j] = dp[3][j+1] + num[j];
        } else
            dp[3][j] = dp[3][j+1];
    }
    for(int i=2;i>=0;i--) {
        for(int j=n;j>=1;j--) {
            if(s[j] == aim[i] && dp[i+1][j] != 1e18) {
                if(dp[i][j+1] == 1e18)
                    dp[i][j] = min((long long)num[j], dp[i+1][j]);
                else
                    dp[i][j] = min(dp[i+1][j],dp[i][j+1] + num[j]);
            } else {
                dp[i][j] = dp[i][j + 1];
            }
        }
    }
}

int main() {
   // freopen("1.in", "r", stdin);

    init();
    DP();
    if(dp[0][1] == 1e18)
        dp[0][1] = 0;
    printf("%lld", dp[0][1]);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
----------- AP11 Results ------------ Pedestrian [email protected], 0.50, 0.50: bbox AP11:0.5897, 0.4874, 0.5217 bev AP11:0.1280, 0.1280, 0.1280 3d AP11:0.1280, 0.1280, 0.1280 aos AP11:0.29, 0.25, 0.25 Pedestrian [email protected], 0.25, 0.25: bbox AP11:0.5897, 0.4874, 0.5217 bev AP11:1.0101, 1.0101, 1.0101 3d AP11:1.0101, 1.0101, 1.0101 aos AP11:0.29, 0.25, 0.25 Cyclist [email protected], 0.50, 0.50: bbox AP11:0.0343, 0.0407, 0.0407 bev AP11:0.0000, 0.0000, 0.0000 3d AP11:0.0000, 0.0000, 0.0000 aos AP11:0.00, 0.01, 0.01 Cyclist [email protected], 0.25, 0.25: bbox AP11:0.0343, 0.0407, 0.0407 bev AP11:0.0125, 0.0145, 0.0145 3d AP11:0.0125, 0.0145, 0.0145 aos AP11:0.00, 0.01, 0.01 Car [email protected], 0.70, 0.70: bbox AP11:0.2165, 4.5455, 4.5455 bev AP11:0.0364, 0.0417, 0.0573 3d AP11:0.0429, 0.0364, 0.0403 aos AP11:0.08, 0.13, 0.17 Car [email protected], 0.50, 0.50: bbox AP11:0.2165, 4.5455, 4.5455 bev AP11:0.7576, 4.5455, 4.5455 3d AP11:0.7576, 4.5455, 4.5455 aos AP11:0.08, 0.13, 0.17 Overall AP11@easy, moderate, hard: bbox AP11:0.2801, 1.6912, 1.7026 bev AP11:0.0548, 0.0566, 0.0618 3d AP11:0.0570, 0.0548, 0.0561 aos AP11:0.12, 0.13, 0.14 ----------- AP40 Results ------------ Pedestrian [email protected], 0.50, 0.50: bbox AP40:0.3140, 0.2965, 0.3301 bev AP40:0.0157, 0.0166, 0.0179 3d AP40:0.0135, 0.0137, 0.0137 aos AP40:0.15, 0.15, 0.16 Pedestrian [email protected], 0.25, 0.25: bbox AP40:0.3140, 0.2965, 0.3301 bev AP40:0.2281, 0.2747, 0.1830 3d AP40:0.2163, 0.2377, 0.1660 aos AP40:0.15, 0.15, 0.16 Cyclist [email protected], 0.50, 0.50: bbox AP40:0.0089, 0.0112, 0.0112 bev AP40:0.0000, 0.0000, 0.0000 3d AP40:0.0000, 0.0000, 0.0000 aos AP40:0.00, 0.00, 0.00 Cyclist [email protected], 0.25, 0.25: bbox AP40:0.0089, 0.0112, 0.0112 bev AP40:0.0034, 0.0040, 0.0040 3d AP40:0.0034, 0.0040, 0.0040 aos AP40:0.00, 0.00, 0.00 Car [email protected], 0.70, 0.70: bbox AP40:0.1004, 0.0867, 0.1244 bev AP40:0.0093, 0.0115, 0.0158 3d AP40:0.0118, 0.0079, 0.0111 aos AP40:0.06, 0.05, 0.07 Car [email protected], 0.50, 0.50: bbox AP40:0.1004, 0.0867, 0.1244 bev AP40:0.1653, 0.1130, 0.1428 3d AP40:0.1344, 0.0911, 0.1118 aos AP40:0.06, 0.05, 0.07 Overall AP40@easy, moderate, hard: bbox AP40:0.1411, 0.1315, 0.1552 bev AP40:0.0083, 0.0094, 0.0112 3d AP40:0.0084, 0.0072, 0.0083 aos AP40:0.07, 0.07, 0.给我解释一下这些参数
最新发布
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值