import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

void main() {
    auto N = readln.chomp.to!int;
    auto S = readln.chomp;
    auto A = readln.split.map!(to!long).array;

    auto dp = new long[][](N+1, 4);
    foreach (i; 0..N+1) fill(dp[i], 1L<<59);
    dp[0][0] = 0;

    foreach (i; 0..N) {
        dp[i+1] = dp[i].dup;
        if (S[i] == 'h') {
            dp[i+1][0] = dp[i][0] + A[i];
            dp[i+1][1] = min(dp[i][0], dp[i][1]);
        } else if (S[i] == 'a') {
            dp[i+1][1] = dp[i][1] + A[i];
            dp[i+1][2] = min(dp[i][1], dp[i][2]);
        } else if (S[i] == 'r') {
            dp[i+1][2] = dp[i][2] + A[i];
            dp[i+1][3] = min(dp[i][2], dp[i][3]);
        } else if (S[i] == 'd') {
            dp[i+1][3] = dp[i][3] + A[i];
        }
    }

    dp[N].reduce!min.writeln;
}
