#include <iostream>
#include <fstream>
#include <list>
#include <stack>
#include <deque>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <ctime>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

#define f first
#define s second
#define pb push_back
#define mp make_pair

const int maxn = 200500;
const int inf = 2e9;
const double eps = 1e-8;
const int base = 1073676287;

vector < int > edge[maxn];
vector < int > leaves;
set < int > newLeaves;
set < int >::iterator it;
int deg[maxn];

void solve( int n, int k ) {
	if ( n == 1 ) {
		leaves.pb( 1 );
		return;
	}
	solve( n - k, min( n - k - 1, k ) );
	int sz = leaves.size();
	newLeaves.clear();
	for ( int j = 0; j < sz; j++ ) {
		edge[leaves[j]].pb( n - k + 1 + j );
		edge[n - k + 1 + j].pb( leaves[j] );
		++deg[leaves[j]];
		if ( deg[leaves[j]] == 1 )
			newLeaves.insert( leaves[j] );
		else
			newLeaves.erase( leaves[j] );

		++deg[n - k + 1 + j];
		if ( deg[n - k + 1 + j] == 1 )
			newLeaves.insert( n - k + 1 + j );
		else
			newLeaves.erase( n - k + 1 + j );
	}

	for ( int j = n - k + 1 + sz; j <= n; j++ ) {
		edge[j].pb( leaves[0] );
		edge[leaves[0]].pb( j );

		++deg[leaves[0]];
		if ( deg[leaves[0]] == 1 )
			newLeaves.insert( leaves[0] );
		else
			newLeaves.erase( leaves[0] );

		++deg[j];
		if ( deg[j] == 1 )
			newLeaves.insert( j );
		else
			newLeaves.erase( j );
	}

	leaves.clear();
	for ( it = newLeaves.begin(); it != newLeaves.end(); it++ )
		leaves.pb( *it );
	// assert( leaves.size() <= k + 1 );
}

int dist[maxn];

void dfs( int v, int anc ) {
	int sz = edge[v].size();
	for ( int j = 0; j < sz; j++ )
		if ( edge[v][j] != anc ) {
			dist[edge[v][j]] = 1 + dist[v];
			dfs( edge[v][j], v );
		}
}

int main()
{
    srand( time( 0 ) );
    // freopen( "input.txt", "r", stdin );
    // freopen( "output.txt", "w", stdout );
    // ios_base::sync_with_stdio(false);
    int n, k;
    scanf ( "%d%d", &n, &k );
    solve( n, k );

    dfs( 1, -1 );
    int v = 1;
    for ( int j = 1; j <= n; j++ )
    	if ( dist[j] > dist[v] )
    		v = j;
    dist[v] = 0;
    dfs( v, -1 );

    v = 1;
    for ( int j = 1; j <= n; j++ )
    	if ( dist[j] > dist[v] )
    		v = j;
    printf( "%d\n", dist[v] );

    for ( int j = 1; j <= n; j++ ) {
    	int sz = edge[j].size();
    	for ( int i = 0; i < sz; i++ )
    		if ( j < edge[j][i] )
    			printf( "%d %d\n", j, edge[j][i] );
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<complex>
using namespace std;
namespace fastIO
{
	#define BUF_SIZE 100000
	#define OUT_SIZE 100000
	#define ll long long
	#define IL inline
	//fread->read
	bool IOerror=0;
	IL char nc()
	{
		static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
		if(p1==pend){p1=buf;pend=buf+fread(buf,1,BUF_SIZE,stdin);if(pend==p1){IOerror=1;return -1;}}
		return *p1++;
	}
	IL bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
	IL void read(int&x)
	{
		bool sign=0;char ch=nc();x=0;for(;blank(ch);ch=nc());if(IOerror)return;
		if(ch=='-')sign=1,ch=nc();for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';if(sign)x=-x;
	}
	IL void read(ll&x)
	{
		bool sign=0;char ch=nc();x=0;for(;blank(ch);ch=nc());if(IOerror)return;
		if(ch=='-')sign=1,ch=nc();for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';if(sign)x=-x;
	}
	IL void read(double&x)
	{
		bool sign=0;char ch=nc();x=0;for(;blank(ch);ch=nc());if(IOerror)return;
		if(ch=='-')sign=1,ch=nc();for(;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
		if(ch=='.'){double tmp=1;ch=nc();for(;ch>='0'&&ch<='9';ch=nc())tmp/=10.0,x+=tmp*(ch-'0');}if(sign)x=-x;
	}
	IL void read(char*s){char ch=nc();for(;blank(ch);ch=nc());if(IOerror)return;for(;!blank(ch)&&!IOerror;ch=nc())*s++=ch;*s=0;}
	IL void read(char&c){for(c=nc();blank(c);c=nc());if(IOerror){c=-1;return;}}
	//getchar->read
	IL void read1(int&x)
	{
		char ch;int bo=0;x=0;for(ch=getchar();ch<'0'||ch>'9';ch=getchar())if(ch=='-')bo=1;
		for(;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());if(bo)x=-x;
	}
	IL void read1(ll&x)
	{
		char ch;int bo=0;x=0;for(ch=getchar();ch<'0'||ch>'9';ch=getchar())if(ch=='-')bo=1;
		for(;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());if(bo)x=-x;
	}
	IL void read1(double&x)
	{
		char ch;int bo=0;x=0;for(ch=getchar();ch<'0'||ch>'9';ch=getchar())if(ch=='-')bo=1;for(;ch>='0'&&ch<='9';x=x*10+ch-'0',ch=getchar());
		if(ch=='.'){double tmp=1;for(ch=getchar();ch>='0'&&ch<='9';tmp/=10.0,x+=tmp*(ch-'0'),ch=getchar());}if(bo)x=-x;
	}
	IL void read1(char*s){char ch=getchar();for(;blank(ch);ch=getchar());for(;!blank(ch);ch=getchar())*s++=ch;*s=0;}
	IL void read1(char&c){for(c=getchar();blank(c);c=getchar());}
	//scanf->read
	IL void read2(int&x){scanf("%d",&x);}
	IL void read2(ll&x)
	{
		#ifdef _WIN32
			scanf("%I64d",&x);
		#else
		#ifdef __linux
			scanf("%lld",&x);
		#else
			puts("error:can't recognize the system!");
		#endif
		#endif
	}
	IL void read2(double&x){scanf("%lf",&x);}
	IL void read2(char*s){scanf("%s",s);}
	IL void read2(char&c){scanf(" %c",&c);}
	IL void readln2(char*s){gets(s);}
	//fwrite->write
	struct Ostream_fwrite
	{
		char *buf,*p1,*pend;
		IL Ostream_fwrite(){buf=new char[BUF_SIZE];p1=buf;pend=buf+BUF_SIZE;}
		IL void out(char ch){if(p1==pend){fwrite(buf,1,BUF_SIZE,stdout);p1=buf;}*p1++=ch;}
		IL void write(int x){static char s[15],*s1;s1=s;if(!x)*s1++='0';if(x<0)out('-'),x=-x;while(x)*s1++=x%10+'0',x/=10;while(s1--!=s)out(*s1);}
		IL void writeln(int x){static char s[15],*s1;s1=s;if(!x)*s1++='0';if(x<0)out('-'),x=-x;while(x)*s1++=x%10+'0',x/=10;while(s1--!=s)out(*s1);out('\n');}
		IL void write(ll x){static char s[25],*s1;s1=s;if(!x)*s1++='0';if(x<0)out('-'),x=-x;while(x)*s1++=x%10+'0',x/=10;while(s1--!=s)out(*s1);}
		IL void writeln(ll x){static char s[25],*s1;s1=s;if(!x)*s1++='0';if(x<0)out('-'),x=-x;while(x)*s1++=x%10+'0',x/=10;while(s1--!=s)out(*s1);out('\n');}
		IL void write(double x,int y)
		{
			static ll mul[]={1,10,100,1000,10000,100000,1000000,10000000,100000000,
				1000000000,10000000000LL,100000000000LL,1000000000000LL,10000000000000LL,
				100000000000000LL,1000000000000000LL,10000000000000000LL,100000000000000000LL};
			if(x<-1e-12)out('-'),x=-x;x*=mul[y];ll x1=(ll)floor(x);if(x-floor(x)>=0.5)++x1;ll x2=x1/mul[y],x3=x1-x2*mul[y];write(x2);
			if(y>0){out('.');for(size_t i=1;i<y&&x3*mul[i]<mul[y];out('0'),++i);write(x3);}
		}
		IL void writeln(double x,int y){write(x,y);out('\n');}
		IL void write(char*s){while(*s)out(*s++);}
		IL void writeln(char*s){while(*s)out(*s++);out('\n');}
		IL void flush(){if(p1!=buf){fwrite(buf,1,p1-buf,stdout);p1=buf;}}
		IL ~Ostream_fwrite(){flush();}
	}Ostream;
	IL void write(int x){Ostream.write(x);}
	IL void writeln(int x){Ostream.writeln(x);}
	IL void write(char x){Ostream.out(x);}
	IL void writeln(char x){Ostream.out(x);Ostream.out('\n');}
	IL void write(ll x){Ostream.write(x);}
	IL void writeln(ll x){Ostream.writeln(x);}
	IL void write(double x,int y){Ostream.write(x,y);}
	IL void writeln(double x,int y){Ostream.writeln(x,y);}
	IL void write(char*s){Ostream.write(s);}
	IL void writeln(char*s){Ostream.writeln(s);}
	IL void writeln(){Ostream.out('\n');}
	IL void flush(){Ostream.flush();}
	//puts->write
	char Out[OUT_SIZE],*o=Out;
	IL void write1(int x){static char buf[15];char *p1=buf;if(!x)*p1++='0';if(x<0)*o++='-',x=-x;while(x)*p1++=x%10+'0',x/=10;while(p1--!=buf)*o++=*p1;}
	IL void writeln1(int x){write1(x);*o++='\n';}
	IL void write1(ll x){static char buf[25];char *p1=buf;if(!x)*p1++='0';if(x<0)*o++='-',x=-x;while(x)*p1++=x%10+'0',x/=10;while(p1--!=buf)*o++=*p1;}
	IL void writeln1(ll x){write1(x);*o++='\n';}
	IL void write1(char c){*o++=c;}
	IL void writeln1(char c){*o++=c;*o++='\n';}
	IL void write1(char*s){while(*s)*o++=*s++;}
	IL void writeln1(char*s){write1(s);*o++='\n';}
	IL void writeln1(){*o++='\n';}
	IL void flush1(){if(o!=Out){if(*(o-1)=='\n')*--o=0;puts(Out);}}
	struct puts_write{IL ~puts_write(){flush1();}}_puts;
	//printf->write
	IL void write2(int x){printf("%d",x);}
	IL void writeln2(int x){printf("%d\n",x);}
	IL void write2(char x){printf("%c",x);}
	IL void writeln2(char x){printf("%c\n",x);}
	IL void write2(ll x)
	{
		#ifdef _WIN32
			printf("%I64d",x);
		#else
		#ifdef __linux
			printf("%lld",x);
		#else
			puts("error:can't recognize the system!");
		#endif
		#endif
	}
	IL void writeln2(ll x){write2(x);printf("\n");}
	IL void writeln2(){printf("\n");}
	#undef IL
	#undef ll
	#undef OUT_SIZE
	#undef BUF_SIZE
};
using namespace fastIO;
int n,k;
int main() {
	scanf("%d%d", &n,&k);
	int p = n-1;
	int q = p / k;
	int r = p % k;
	int ans = q + q;
	if(r) ++ans;
	if(r>=2) ++ans;
	printf("%d\n", ans);
	for(int i = 1; i <= k; ++i) {
		printf("%d %d\n", i, n);
	}
	for(int i = k+1; i < n; ++i) {
		printf("%d %d\n", i, i - k);
	}
	return 0;
}


/*
  Creator :  bizarre
*/
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define bizarre  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define ll long long
#define ld long double
#define pi acos(-1)
#define point complex<double>
const int N = 2e3 + 5, inf = 2e9, mod = 1e9 + 7;
vector<int> prime_Numbers;
bool primes[1000005];
double dot(point a, point b)
{
    return (conj(a) * b).real();
}
double cross(point a, point b)
{
    return (conj(a) * b).imag();
}
/*
void prime(int n)
{
    primes[1]=primes[0]=0;
    for(int i=2; i<=n; i++)
        primes[i]=true;
    for(int i=2; i<=n; i++)
    {
        if(primes[i]==true)
        {
            prime_Numbers.push_back(i);
            for(int j=i+i; j<=n; j+=i)
                primes[j]=false;
        }
    }
}
void move1step(long long& a, long long& b, long long q)
{
    long long c= a-q*b;
    a=b;
    b=c;
}
long long egcd(long long r0,long long r1,long long& x0,long long& y0)
{
    long long x1=0,y1=1;
    x0=1;
    y0=0;
    while(r1)
    {
        long long q=r0/r1;
        move1step(r0,r1,q);
        move1step(x0,x1,q);
        move1step(y0,y1,q);
    }
    return r0;
}
bool  solve (long long a, long long b, long long c,long long&x, long long& y)
{
    long long g=egcd(a,b,x,y);
    int m=c/g;
    x*=m;
    y*=m;
    return c%g==0;
}
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
void PrimeFactorization(int n)
{
    for(int i=2; i*i<=n; i++)
    {
        int c=0;
        if(n%i==0)
        {
            while(n%i==0)
            {
                n/=i;
            }

        }
    }

}
long long ncrtane(long long n, long long r)
{
    long long ans=1,cntr=1;
    for(int i=n; i>n-r; i--)
    {
        ans*=i;
        ans/=cntr;
        cntr++;
    }
    return ans;
}
int ncr(long long n, long long r)
{
    return (((fact[n] * inv[n - r]) % mod) * inv[r]) % mod;
}

int npr(long long n, long long r)
{
    return (fact[n] * inv[n - r]) % mod;
}
long long fact[N], inv[N];
void pre(long long n)
{
    fact[0] = 1;
    inv[0] = 1;
    for (long long i = 1; i <= n; i++)
    {
        fact[i] = (i * fact[i - 1]) % mod;
        inv[i] = fp(fact[i], mod - 2);
    }
}

long long fp(long long base, long long exp)
{
    if (exp == 0)
        return 1;
    long long ans = fp(base, exp / 2);
    ans = (ans * ans) % mod;
    if (exp % 2 != 0)
        ans = (ans * (base % mod)) % mod;
    return ans;
}
*/
int n;
string s;
ll a[100005];
ll dp[100005][5];
ll solve(int indx, int lst)
{
    if(indx==n)
    {
        if(lst==4)return 1e18;
        else return 0;
    }
    if(lst==4)return 1e18;
    if(~dp[indx][lst])return dp[indx][lst];
    ll ans = 0;
    if(s[indx]=='h'&&lst==0)ans = min(solve(indx+1,1),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='a'&&lst==1)ans = min(solve(indx+1,2),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='r'&&lst==2)ans = min(solve(indx+1,3),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='d'&&lst==3)ans = min(solve(indx+1,4),solve(indx+1,lst)+a[indx]);
    else  ans = solve(indx+1,lst);
    return dp[indx][lst]=ans;
}
int main()
{
    bizarre
    memset(dp,-1,sizeof dp);
    cin >> n >> s;
    for(int i = 0;i<n;i++)cin >> a[i];
    cout << solve(0,0);
    return 0;
}


/*
  Creator :  bizarre
*/
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define bizarre  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define ll long long
#define ld long double
#define pi acos(-1)
#define point complex<double>
const int N = 2e3 + 5, inf = 2e9, mod = 1e9 + 7;
vector<int> prime_Numbers;
bool primes[1000005];
double dot(point a, point b)
{
    return (conj(a) * b).real();
}
double cross(point a, point b)
{
    return (conj(a) * b).imag();
}
/*
void prime(int n)
{
    primes[1]=primes[0]=0;
    for(int i=2; i<=n; i++)
        primes[i]=true;
    for(int i=2; i<=n; i++)
    {
        if(primes[i]==true)
        {
            prime_Numbers.push_back(i);
            for(int j=i+i; j<=n; j+=i)
                primes[j]=false;
        }
    }
}
void move1step(long long& a, long long& b, long long q)
{
    long long c= a-q*b;
    a=b;
    b=c;
}
long long egcd(long long r0,long long r1,long long& x0,long long& y0)
{
    long long x1=0,y1=1;
    x0=1;
    y0=0;
    while(r1)
    {
        long long q=r0/r1;
        move1step(r0,r1,q);
        move1step(x0,x1,q);
        move1step(y0,y1,q);
    }
    return r0;
}
bool  solve (long long a, long long b, long long c,long long&x, long long& y)
{
    long long g=egcd(a,b,x,y);
    int m=c/g;
    x*=m;
    y*=m;
    return c%g==0;
}
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
void PrimeFactorization(int n)
{
    for(int i=2; i*i<=n; i++)
    {
        int c=0;
        if(n%i==0)
        {
            while(n%i==0)
            {
                n/=i;
            }

        }
    }

}
long long ncrtane(long long n, long long r)
{
    long long ans=1,cntr=1;
    for(int i=n; i>n-r; i--)
    {
        ans*=i;
        ans/=cntr;
        cntr++;
    }
    return ans;
}
int ncr(long long n, long long r)
{
    return (((fact[n] * inv[n - r]) % mod) * inv[r]) % mod;
}

int npr(long long n, long long r)
{
    return (fact[n] * inv[n - r]) % mod;
}
long long fact[N], inv[N];
void pre(long long n)
{
    fact[0] = 1;
    inv[0] = 1;
    for (long long i = 1; i <= n; i++)
    {
        fact[i] = (i * fact[i - 1]) % mod;
        inv[i] = fp(fact[i], mod - 2);
    }
}

long long fp(long long base, long long exp)
{
    if (exp == 0)
        return 1;
    long long ans = fp(base, exp / 2);
    ans = (ans * ans) % mod;
    if (exp % 2 != 0)
        ans = (ans * (base % mod)) % mod;
    return ans;
}
*/
int n;
string s;
int a[100005];
int dp[100005][5];
ll solve(int indx, int lst)
{
    if(indx==n)
    {
        if(lst==4)return 1e18;
        else return 0;
    }
    if(lst==4)return 1e18;
    if(~dp[indx][lst])return dp[indx][lst];
    ll ans = 0;
    if(s[indx]=='h')ans = min(solve(indx+1,1),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='a'&&lst==1)ans = min(solve(indx+1,2),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='r'&&lst==2)ans = min(solve(indx+1,3),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='d'&&lst==3)ans = min(solve(indx+1,4),solve(indx+1,lst)+a[indx]);
    else return solve(indx+1,lst);
    return dp[indx][lst]=ans;
}
int main()
{
    bizarre
    memset(dp,-1,sizeof dp);
    cin >> n >> s;
    for(int i = 0;i<n;i++)cin >> a[i];
    cout << solve(0,0);
    return 0;
}


/*
  Creator :  bizarre
*/
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define bizarre  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define ll long long
#define ld long double
#define pi acos(-1)
#define point complex<double>
const int N = 2e3 + 5, inf = 2e9, mod = 1e9 + 7;
vector<int> prime_Numbers;
bool primes[1000005];
double dot(point a, point b)
{
    return (conj(a) * b).real();
}
double cross(point a, point b)
{
    return (conj(a) * b).imag();
}
/*
void prime(int n)
{
    primes[1]=primes[0]=0;
    for(int i=2; i<=n; i++)
        primes[i]=true;
    for(int i=2; i<=n; i++)
    {
        if(primes[i]==true)
        {
            prime_Numbers.push_back(i);
            for(int j=i+i; j<=n; j+=i)
                primes[j]=false;
        }
    }
}
void move1step(long long& a, long long& b, long long q)
{
    long long c= a-q*b;
    a=b;
    b=c;
}
long long egcd(long long r0,long long r1,long long& x0,long long& y0)
{
    long long x1=0,y1=1;
    x0=1;
    y0=0;
    while(r1)
    {
        long long q=r0/r1;
        move1step(r0,r1,q);
        move1step(x0,x1,q);
        move1step(y0,y1,q);
    }
    return r0;
}
bool  solve (long long a, long long b, long long c,long long&x, long long& y)
{
    long long g=egcd(a,b,x,y);
    int m=c/g;
    x*=m;
    y*=m;
    return c%g==0;
}
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
void PrimeFactorization(int n)
{
    for(int i=2; i*i<=n; i++)
    {
        int c=0;
        if(n%i==0)
        {
            while(n%i==0)
            {
                n/=i;
            }

        }
    }

}
long long ncrtane(long long n, long long r)
{
    long long ans=1,cntr=1;
    for(int i=n; i>n-r; i--)
    {
        ans*=i;
        ans/=cntr;
        cntr++;
    }
    return ans;
}
int ncr(long long n, long long r)
{
    return (((fact[n] * inv[n - r]) % mod) * inv[r]) % mod;
}

int npr(long long n, long long r)
{
    return (fact[n] * inv[n - r]) % mod;
}
long long fact[N], inv[N];
void pre(long long n)
{
    fact[0] = 1;
    inv[0] = 1;
    for (long long i = 1; i <= n; i++)
    {
        fact[i] = (i * fact[i - 1]) % mod;
        inv[i] = fp(fact[i], mod - 2);
    }
}

long long fp(long long base, long long exp)
{
    if (exp == 0)
        return 1;
    long long ans = fp(base, exp / 2);
    ans = (ans * ans) % mod;
    if (exp % 2 != 0)
        ans = (ans * (base % mod)) % mod;
    return ans;
}
*/
int n;
string s;
ll a[100005];
ll dp[100005][5];
ll solve(int indx, int lst)
{
    if(indx==n)
    {
        if(lst==4)return 1e18;
        else return 0;
    }
    if(lst==4)return 1e18;
    if(~dp[indx][lst])return dp[indx][lst];
    ll ans = 0;
    if(s[indx]=='h')ans = min(solve(indx+1,1),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='a'&&lst==1)ans = min(solve(indx+1,2),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='r'&&lst==2)ans = min(solve(indx+1,3),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='d'&&lst==3)ans = min(solve(indx+1,4),solve(indx+1,lst)+a[indx]);
    else  ans = solve(indx+1,lst);
    return dp[indx][lst]=ans;
}
int main()
{
    bizarre
    memset(dp,-1,sizeof dp);
    cin >> n >> s;
    for(int i = 0;i<n;i++)cin >> a[i];
    cout << solve(0,0);
    return 0;
}


/*
  Creator :  bizarre
*/
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define bizarre  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define ll long long
#define ld long double
#define pi acos(-1)
#define point complex<double>
const int N = 2e3 + 5, inf = 2e9, mod = 1e9 + 7;
vector<int> prime_Numbers;
bool primes[1000005];
double dot(point a, point b)
{
    return (conj(a) * b).real();
}
double cross(point a, point b)
{
    return (conj(a) * b).imag();
}
/*
void prime(int n)
{
    primes[1]=primes[0]=0;
    for(int i=2; i<=n; i++)
        primes[i]=true;
    for(int i=2; i<=n; i++)
    {
        if(primes[i]==true)
        {
            prime_Numbers.push_back(i);
            for(int j=i+i; j<=n; j+=i)
                primes[j]=false;
        }
    }
}
void move1step(long long& a, long long& b, long long q)
{
    long long c= a-q*b;
    a=b;
    b=c;
}
long long egcd(long long r0,long long r1,long long& x0,long long& y0)
{
    long long x1=0,y1=1;
    x0=1;
    y0=0;
    while(r1)
    {
        long long q=r0/r1;
        move1step(r0,r1,q);
        move1step(x0,x1,q);
        move1step(y0,y1,q);
    }
    return r0;
}
bool  solve (long long a, long long b, long long c,long long&x, long long& y)
{
    long long g=egcd(a,b,x,y);
    int m=c/g;
    x*=m;
    y*=m;
    return c%g==0;
}
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
void PrimeFactorization(int n)
{
    for(int i=2; i*i<=n; i++)
    {
        int c=0;
        if(n%i==0)
        {
            while(n%i==0)
            {
                n/=i;
            }

        }
    }

}
long long ncrtane(long long n, long long r)
{
    long long ans=1,cntr=1;
    for(int i=n; i>n-r; i--)
    {
        ans*=i;
        ans/=cntr;
        cntr++;
    }
    return ans;
}
int ncr(long long n, long long r)
{
    return (((fact[n] * inv[n - r]) % mod) * inv[r]) % mod;
}

int npr(long long n, long long r)
{
    return (fact[n] * inv[n - r]) % mod;
}
long long fact[N], inv[N];
void pre(long long n)
{
    fact[0] = 1;
    inv[0] = 1;
    for (long long i = 1; i <= n; i++)
    {
        fact[i] = (i * fact[i - 1]) % mod;
        inv[i] = fp(fact[i], mod - 2);
    }
}

long long fp(long long base, long long exp)
{
    if (exp == 0)
        return 1;
    long long ans = fp(base, exp / 2);
    ans = (ans * ans) % mod;
    if (exp % 2 != 0)
        ans = (ans * (base % mod)) % mod;
    return ans;
}
*/
int n;
string s;
ll a[100005];
ll dp[100005][5];
ll solve(int indx, int lst)
{
    if(indx==n)
    {
        if(lst==4)return 1e18;
        else return 0;
    }
    if(lst==4)return 1e18;
    if(~dp[indx][lst])return dp[indx][lst];
    ll ans = 0;
    if(s[indx]=='h')ans = min(solve(indx+1,1),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='a'&&lst==1)ans = min(solve(indx+1,2),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='r'&&lst==2)ans = min(solve(indx+1,3),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='d'&&lst==3)ans = min(solve(indx+1,4),solve(indx+1,lst)+a[indx]);
    else return solve(indx+1,lst);
    return dp[indx][lst]=ans;
}
int main()
{
    bizarre
    memset(dp,-1,sizeof dp);
    cin >> n >> s;
    for(int i = 0;i<n;i++)cin >> a[i];
    cout << solve(0,0);
    return 0;
}


/*
  Creator :  bizarre
*/
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define bizarre  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define ll long long
#define ld long double
#define pi acos(-1)
#define point complex<double>
const int N = 2e3 + 5, inf = 2e9, mod = 1e9 + 7;
vector<int> prime_Numbers;
bool primes[1000005];
double dot(point a, point b)
{
    return (conj(a) * b).real();
}
double cross(point a, point b)
{
    return (conj(a) * b).imag();
}
/*
void prime(int n)
{
    primes[1]=primes[0]=0;
    for(int i=2; i<=n; i++)
        primes[i]=true;
    for(int i=2; i<=n; i++)
    {
        if(primes[i]==true)
        {
            prime_Numbers.push_back(i);
            for(int j=i+i; j<=n; j+=i)
                primes[j]=false;
        }
    }
}
void move1step(long long& a, long long& b, long long q)
{
    long long c= a-q*b;
    a=b;
    b=c;
}
long long egcd(long long r0,long long r1,long long& x0,long long& y0)
{
    long long x1=0,y1=1;
    x0=1;
    y0=0;
    while(r1)
    {
        long long q=r0/r1;
        move1step(r0,r1,q);
        move1step(x0,x1,q);
        move1step(y0,y1,q);
    }
    return r0;
}
bool  solve (long long a, long long b, long long c,long long&x, long long& y)
{
    long long g=egcd(a,b,x,y);
    int m=c/g;
    x*=m;
    y*=m;
    return c%g==0;
}
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
void PrimeFactorization(int n)
{
    for(int i=2; i*i<=n; i++)
    {
        int c=0;
        if(n%i==0)
        {
            while(n%i==0)
            {
                n/=i;
            }

        }
    }

}
long long ncrtane(long long n, long long r)
{
    long long ans=1,cntr=1;
    for(int i=n; i>n-r; i--)
    {
        ans*=i;
        ans/=cntr;
        cntr++;
    }
    return ans;
}
int ncr(long long n, long long r)
{
    return (((fact[n] * inv[n - r]) % mod) * inv[r]) % mod;
}

int npr(long long n, long long r)
{
    return (fact[n] * inv[n - r]) % mod;
}
long long fact[N], inv[N];
void pre(long long n)
{
    fact[0] = 1;
    inv[0] = 1;
    for (long long i = 1; i <= n; i++)
    {
        fact[i] = (i * fact[i - 1]) % mod;
        inv[i] = fp(fact[i], mod - 2);
    }
}

long long fp(long long base, long long exp)
{
    if (exp == 0)
        return 1;
    long long ans = fp(base, exp / 2);
    ans = (ans * ans) % mod;
    if (exp % 2 != 0)
        ans = (ans * (base % mod)) % mod;
    return ans;
}
*/
int n;
string s;
ll a[100005];
ll dp[100005][5];
ll solve(int indx, int lst)
{
    if(indx==n)
    {
        if(lst==4)return 1e18;
        else return 0;
    }
    if(lst==4)return 1e18;
    if(~dp[indx][lst])return dp[indx][lst];
    ll ans = 0;
    if(s[indx]=='h'&&lst==0)ans = min(solve(indx+1,1),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='a'&&lst==1)ans = min(solve(indx+1,2),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='r'&&lst==2)ans = min(solve(indx+1,3),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='d'&&lst==3)ans = min(solve(indx+1,4),solve(indx+1,lst)+a[indx]);
    else  solve(indx+1,lst);
    return dp[indx][lst]=ans;
}
int main()
{
    bizarre
    memset(dp,-1,sizeof dp);
    cin >> n >> s;
    for(int i = 0;i<n;i++)cin >> a[i];
    cout << solve(0,0);
    return 0;
}


/*
  Creator :  bizarre
*/
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define bizarre  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
#define ll long long
#define ld long double
#define pi acos(-1)
#define point complex<double>
const int N = 2e3 + 5, inf = 2e9, mod = 1e9 + 7;
vector<int> prime_Numbers;
bool primes[1000005];
double dot(point a, point b)
{
    return (conj(a) * b).real();
}
double cross(point a, point b)
{
    return (conj(a) * b).imag();
}
/*
void prime(int n)
{
    primes[1]=primes[0]=0;
    for(int i=2; i<=n; i++)
        primes[i]=true;
    for(int i=2; i<=n; i++)
    {
        if(primes[i]==true)
        {
            prime_Numbers.push_back(i);
            for(int j=i+i; j<=n; j+=i)
                primes[j]=false;
        }
    }
}
void move1step(long long& a, long long& b, long long q)
{
    long long c= a-q*b;
    a=b;
    b=c;
}
long long egcd(long long r0,long long r1,long long& x0,long long& y0)
{
    long long x1=0,y1=1;
    x0=1;
    y0=0;
    while(r1)
    {
        long long q=r0/r1;
        move1step(r0,r1,q);
        move1step(x0,x1,q);
        move1step(y0,y1,q);
    }
    return r0;
}
bool  solve (long long a, long long b, long long c,long long&x, long long& y)
{
    long long g=egcd(a,b,x,y);
    int m=c/g;
    x*=m;
    y*=m;
    return c%g==0;
}
int gcd(int a, int b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
void PrimeFactorization(int n)
{
    for(int i=2; i*i<=n; i++)
    {
        int c=0;
        if(n%i==0)
        {
            while(n%i==0)
            {
                n/=i;
            }

        }
    }

}
long long ncrtane(long long n, long long r)
{
    long long ans=1,cntr=1;
    for(int i=n; i>n-r; i--)
    {
        ans*=i;
        ans/=cntr;
        cntr++;
    }
    return ans;
}
int ncr(long long n, long long r)
{
    return (((fact[n] * inv[n - r]) % mod) * inv[r]) % mod;
}

int npr(long long n, long long r)
{
    return (fact[n] * inv[n - r]) % mod;
}
long long fact[N], inv[N];
void pre(long long n)
{
    fact[0] = 1;
    inv[0] = 1;
    for (long long i = 1; i <= n; i++)
    {
        fact[i] = (i * fact[i - 1]) % mod;
        inv[i] = fp(fact[i], mod - 2);
    }
}

long long fp(long long base, long long exp)
{
    if (exp == 0)
        return 1;
    long long ans = fp(base, exp / 2);
    ans = (ans * ans) % mod;
    if (exp % 2 != 0)
        ans = (ans * (base % mod)) % mod;
    return ans;
}
*/
int n;
string s;
ll a[100005];
ll dp[100005][5];
ll solve(int indx, int lst)
{
    if(indx==n)
    {
        if(lst==4)return 1e18;
        else return 0;
    }
    if(lst==4)return 1e18;
    if(~dp[indx][lst])return dp[indx][lst];
    ll ans = 0;
    if(s[indx]=='h'&&lst==0)ans = min(solve(indx+1,1),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='a'&&lst==1)ans = min(solve(indx+1,2),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='r'&&lst==2)ans = min(solve(indx+1,3),solve(indx+1,lst)+a[indx]);
    else if(s[indx]=='d'&&lst==3)ans = min(solve(indx+1,4),solve(indx+1,lst)+a[indx]);
    else return solve(indx+1,lst);
    return dp[indx][lst]=ans;
}
int main()
{
    bizarre
    memset(dp,-1,sizeof dp);
    cin >> n >> s;
    for(int i = 0;i<n;i++)cin >> a[i];
    cout << solve(0,0);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define maxn 100010
#define maxm 1000010
#define mod 233
typedef long long ll;
ll INF=(1e18);
char str[maxn];
int n;

int main()
{
	/*std::ios::sync_with_stdio(0);*/
	int n;
	scanf("%d",&n);
	scanf("%s",str+1);
	ll a=0,b=0,c=0,d=0;
	for(int i=1;i<=n;i++)
	{
		ll v;scanf("%lld",&v);
		if(str[i]=='h')
		{
			a+=v;
		}
		else if(str[i]=='a')
		{
			b=min(b+v,a);
		}
		else if(str[i]=='r')
		{
			c=min(c+v,b);
		}
		else if(str[i]=='d')
		{
			d=min(d+v,c);
		}
	}
	printf("%lld\n",d);
	return 0;
}


#include <cstdio>
#include <climits>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

const int MAXN = 100005;

char str[MAXN];
long long a[MAXN], f[MAXN], sum[4];

bool hard(char c) {
    return c == 'h' || c == 'a' || c == 'r' || c == 'd';
}

int main() {
    int n;
    scanf("%d %s", &n, str);
    for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

    int p = 0;
    for (int i = 0; i < n; i++) if (hard(str[i])) {
            str[p] = str[i];
            a[p] = a[i];
            ++p;
    }

    for (int i = p - 1; ~i; i--) {
        if (str[i] == 'h') {
            if (a[i] >= sum[1]) {
                f[i] = sum[1];
                sum[1] = 0;
            } else {
                f[i] = a[i];
            }
            sum[0] += f[i];
        }
        if (str[i] == 'a') {
            if (a[i] >= sum[2]) {
                f[i] = sum[2];
                sum[2] = 0;
            } else {
                f[i] = a[i];
            }
            sum[1] += f[i];
        }
        if (str[i] == 'r') {
            if (a[i] >= sum[3]) {
                f[i] = sum[3];
                sum[3] = 0;
            } else {
                f[i] = a[i];
            }
            sum[2] += f[i];
        }
        if (str[i] == 'd') {
            f[i] = a[i];
            sum[3] += f[i];
        }
    }

    printf("%lld\n", sum[0]);

    return 0;
}


#include <cstdio>
#include <climits>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

const int MAXN = 100005;

char str[MAXN];
long long a[MAXN], f[MAXN], sum[4];

bool hard(char c) {
    return c == 'h' || c == 'a' || c == 'r' || c == 'd';
}

int main() {
    int n;
    scanf("%d %s", &n, str);
    for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

    int p = 0;
    for (int i = 0; i < n; i++) if (hard(str[i])) {
        if (p && str[i] == str[p - 1]) {
            a[p - 1] += a[i];
        } else {
            str[p] = str[i];
            a[p] = a[i];
            ++p;
        }
    }

    for (int i = p - 1; ~i; i--) {
        if (str[i] == 'h') {
            if (a[i] >= sum[1]) {
                f[i] = sum[1];
                sum[1] = 0;
            } else {
                f[i] = a[i];
            }
            sum[0] += f[i];
        }
        if (str[i] == 'a') {
            if (a[i] >= sum[2]) {
                f[i] = sum[2];
                sum[2] = 0;
            } else {
                f[i] = a[i];
            }
            sum[1] += f[i];
        }
        if (str[i] == 'r') {
            if (a[i] >= sum[3]) {
                f[i] = sum[3];
                sum[3] = 0;
            } else {
                f[i] = a[i];
            }
            sum[2] += f[i];
        }
        if (str[i] == 'd') {
            f[i] = a[i];
            sum[3] += f[i];
        }
    }

    long long ans = 0;
    for (int i = 0; i < p; i++) if (str[i] == 'h') ans += f[i];
    printf("%lld\n", ans);

    return 0;
}


#include <cstdio>
#include <climits>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

const int MAXN = 100005;

char str[MAXN];
long long a[MAXN], f[MAXN], sum[4];

bool hard(char c) {
    return c == 'h' || c == 'a' || c == 'r' || c == 'd';
}

int main() {
    int n;
    scanf("%d %s", &n, str);
    for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

    int p = 0;
    for (int i = 0; i < n; i++) if (hard(str[i])) {
        if (p && str[i] == str[p - 1]) {
            a[p - 1] += a[i];
        } else {
            str[p] = str[i];
            a[p] = a[i];
            ++p;
        }
    }

    for (int i = p - 1; ~i; i--) {
        if (str[i] == 'h') {
            if (a[i] < sum[1]) {
                f[i] = a[i];
                sum[1] = 0;
            } else {
                f[i] = sum[1];
            }
            sum[0] += f[i];
        }
        if (str[i] == 'a') {
            if (a[i] < sum[2]) {
                f[i] = a[i];
                sum[2] = 0;
            } else {
                f[i] = sum[2];
            }
            sum[1] += f[i];
        }
        if (str[i] == 'r') {
            if (a[i] < sum[3]) {
                f[i] = a[i];
                sum[3] = 0;
            } else {
                f[i] = sum[3];
            }
            sum[2] += f[i];
        }
        if (str[i] == 'd') {
            f[i] = a[i];
            sum[3] += f[i];
        }
    }

    long long ans = 0;
    for (int i = 0; i < p; i++) if (str[i] == 'h') ans += f[i];
    printf("%lld\n", ans);

    return 0;
}


#include <cstdio>
#include <climits>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

const int MAXN = 100005;

char str[MAXN];
long long a[MAXN], f[MAXN], sum[4];

bool hard(char c) {
    return c == 'h' || c == 'a' || c == 'r' || c == 'd';
}

int main() {
    int n;
    scanf("%d %s", &n, str);
    for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

    int p = 0;
    for (int i = 0; i < n; i++) if (hard(str[i])) {
        if (p && str[i] == str[p - 1]) {
            a[p - 1] += a[i];
        } else {
            str[p] = str[i];
            a[p] = a[i];
            ++p;
        }
    }

    for (int i = p - 1; ~i; i--) {
        if (str[i] == 'h') {
            f[i] = std::min(a[i], sum[1]);
            sum[0] += f[i];
        }
        if (str[i] == 'a') {
            f[i] = std::min(a[i], sum[2]);
            sum[1] += f[i];
        }
        if (str[i] == 'r') {
            f[i] = std::min(a[i], sum[3]);
            sum[2] += f[i];
        }
        if (str[i] == 'd') {
            f[i] = a[i];
            sum[3] += f[i];
        }
    }

    long long ans = 0;
    for (int i = 0; i < p; i++) if (str[i] == 'h') ans += f[i];
    printf("%lld\n", ans);

    return 0;
}


#include <cstdio>
#include <climits>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

const int MAXN = 100005;

char str[MAXN];
long long a[MAXN], f[MAXN], sum[4];

bool hard(char c) {
    return c == 'h' || c == 'a' || c == 'r' || c == 'd';
}

int main() {
    int n;
    scanf("%d %s", &n, str);
    for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

    int p = 0;
    for (int i = 0; i < n; i++) if (hard(str[i])) {

            str[p] = str[i];
            a[p] = a[i];
            ++p;

    }

    for (int i = p - 1; ~i; i--) {
        if (str[i] == 'h') {
            f[i] = std::min(a[i], sum[1]);
            sum[0] += f[i];
        }
        if (str[i] == 'a') {
            f[i] = std::min(a[i], sum[2]);
            sum[1] += f[i];
        }
        if (str[i] == 'r') {
            f[i] = std::min(a[i], sum[3]);
            sum[2] += f[i];
        }
        if (str[i] == 'd') {
            f[i] = a[i];
            sum[3] += f[i];
        }
    }

    long long ans = 0;
    for (int i = 0; i < p; i++) if (str[i] == 'h') ans += f[i];
    printf("%lld\n", ans);

    return 0;
}


#include <cstdio>
#include <climits>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

const int MAXN = 100005;

char str[MAXN];
long long a[MAXN], f[MAXN], sum[4];

bool hard(char c) {
    return c == 'h' || c == 'a' || c == 'r' || c == 'd';
}

int main() {
    int n;
    scanf("%d %s", &n, str);
    for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

    int p = 0;
    for (int i = 0; i < n; i++) if (hard(str[i])) {
        if (p && str[i] == str[p - 1]) {
            a[p - 1] += a[i];
        } else {
            str[p] = str[i];
            a[p] = a[i];
            ++p;
        }
    }

    for (int i = p - 1; ~i; i--) {
        if (str[i] == 'h') {
            if (a[i] > sum[1]) {
                f[i] = sum[1];
                sum[1] = 0;
            } else {
                f[i] = a[i];
            }
            sum[0] += f[i];
        }
        if (str[i] == 'a') {
            if (a[i] > sum[2]) {
                f[i] = sum[2];
                sum[2] = 0;
            } else {
                f[i] = a[i];
            }
            sum[1] += f[i];
        }
        if (str[i] == 'r') {
            if (a[i] > sum[3]) {
                f[i] = sum[3];
                sum[3] = 0;
            } else {
                f[i] = a[i];
            }
            sum[2] += f[i];
        }
        if (str[i] == 'd') {
            f[i] = a[i];
            sum[3] += f[i];
        }
    }

    long long ans = 0;
    for (int i = 0; i < p; i++) if (str[i] == 'h') ans += f[i];
    printf("%lld\n", ans);

    return 0;
}


#include <cstdio>
#include <climits>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

const int MAXN = 100005;

char str[MAXN];
long long a[MAXN], f[MAXN], sum[4];

bool hard(char c) {
    return c == 'h' || c == 'a' || c == 'r' || c == 'd';
}

int main() {
    int n;
    scanf("%d %s", &n, str);
    for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

    int p = 0;
    for (int i = 0; i < n; i++) if (hard(str[i])) {
        if (str[i] == str[p - 1]) {
            a[p - 1] += a[i];
        } else {
            str[p] = str[i];
            a[p] = a[i];
            ++p;
        }
    }

    for (int i = p - 1; ~i; i--) {
        if (str[i] == 'h') {
            f[i] = std::min(a[i], sum[1]);
            sum[0] += f[i];
        }
        if (str[i] == 'a') {
            f[i] = std::min(a[i], sum[2]);
            sum[1] += f[i];
        }
        if (str[i] == 'r') {
            f[i] = std::min(a[i], sum[3]);
            sum[2] += f[i];
        }
        if (str[i] == 'd') {
            f[i] = a[i];
            sum[3] += f[i];
        }
    }

    long long ans = 0;
    for (int i = 0; i < p; i++) if (str[i] == 'h') ans += f[i];
    printf("%lld\n", ans);

    return 0;
}


#include <cstdio>
#include <climits>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>

const int MAXN = 100005;

char str[MAXN];
long long a[MAXN], f[MAXN], sum[4];

bool hard(char c) {
    return c == 'h' || c == 'a' || c == 'r' || c == 'd';
}

int main() {
    int n;
    scanf("%d %s", &n, str);
    for (int i = 0; i < n; i++) scanf("%lld", &a[i]);

    int p = 0;
    for (int i = 0; i < n; i++) if (hard(str[i])) {
        if (p && str[i] == str[p - 1]) {
            a[p - 1] += a[i];
        } else {
            str[p] = str[i];
            a[p] = a[i];
            ++p;
        }
    }

    for (int i = p - 1; ~i; i--) {
        if (str[i] == 'h') {
            if (a[i] <= sum[1]) {
                f[i] = a[i];
                sum[1] = 0;
            } else {
                f[i] = sum[1];
            }
            sum[0] += f[i];
        }
        if (str[i] == 'a') {
            if (a[i] <= sum[2]) {
                f[i] = a[i];
                sum[2] = 0;
            } else {
                f[i] = sum[2];
            }
            sum[1] += f[i];
        }
        if (str[i] == 'r') {
            if (a[i] <= sum[3]) {
                f[i] = a[i];
                sum[3] = 0;
            } else {
                f[i] = sum[3];
            }
            sum[2] += f[i];
        }
        if (str[i] == 'd') {
            f[i] = a[i];
            sum[3] += f[i];
        }
    }

    long long ans = 0;
    for (int i = 0; i < p; i++) if (str[i] == 'h') ans += f[i];
    printf("%lld\n", ans);

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define Int long long
#define N 100100

int a[N];
Int dp[N][4];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    string s2="hard";
    for(int i = 0;i<n;i++)cin>>a[i];
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<4;j++)
        {
            if(s[i]==s2[j]){if(j)dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+a[i]);else  dp[i][j]=dp[i-1][j]+a[i];}
            else dp[i][j]=dp[i-1][j];
        }
    }
    cout<<dp[n-1][3]<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define Int long long
#define N 100100

Int a[N];
Int dp[N][4];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    string s2="hard";
    for(int i = 0;i<n;i++)cin>>a[i];
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<4;j++)
        {
            if(s[i]==s2[j]){
                    if(j>0)dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+a[i]);
                else if(i) dp[i][j]=dp[i-1][j]+a[i];else dp[i][j]=a[i];
            }
            else dp[i][j]=dp[i-1][j];
        }
    }
    cout<<dp[n-1][3]<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define Int long long
#define N 100100

Int a[N];
Int dp[N][4];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    string s2="hard";
    for(int i = 0;i<n;i++)cin>>a[i];
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<4;j++)
        {
            if(s[i]==s2[j]){if(j>0)dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+a[i]);else  dp[i][j]=dp[i-1][j]+a[i];}
            else dp[i][j]=dp[i-1][j];
        }
    }
    cout<<dp[n-1][3]<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define Int long long
#define N 100100

Int a[N];
Int dp[N][4];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    string s2="hard";
    for(int i = 0;i<n;i++)cin>>a[i];
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<4;j++)
        {
            if(s[i]==s2[j]){
                    if(j>0)dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+a[i]);
            else  dp[i][j]=dp[i-1][j]+a[i];
            }
            else dp[i][j]=dp[i-1][j];
        }
    }
    cout<<dp[n-1][3]<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define Int long long
#define N 100100

Int a[N];
Int dp[N][4];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    string s2="hard";
    for(int i = 0;i<n;i++)cin>>a[i];
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<4;j++)
        {
            if(s[i]==s2[j]){
                    if(j>0)dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+a[i]);
                else if(i) dp[i][j]=dp[i-1][j]+a[i];else dp[i][j]=a[i];
            }
            else {if(i)dp[i][j]=dp[i-1][j];else dp[i][j]=0;}
        }
    }
    cout<<dp[n-1][3]<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define Int long long
#define N 100100

Int a[N];
Int dp[N][4];

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n;
    cin>>n;
    string s;
    cin>>s;
    string s2="hard";
    for(int i = 0;i<n;i++)cin>>a[i];
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<4;j++)
        {
            if(s[i]==s2[j]){if(j)dp[i][j]=min(dp[i-1][j-1],dp[i-1][j]+a[i]);else  dp[i][j]=dp[i-1][j]+a[i];}
            else dp[i][j]=dp[i-1][j];
        }
    }
    cout<<dp[n-1][3]<<endl;
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
#define lowbit(x) (x)&(-x)
typedef long long ll;
//
using namespace std;
const int maxn = 1e5+ 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9+7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll lcm(ll a, ll b)
{
return a / gcd(a, b)*b;
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	a = a%mod;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
	a = (a*a) % mod;
	}
	return ans;
}
/*ll C(ll n, ll m)//������ļ���
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = (ans*(n - m + i) / i)%mod;
	return ans%mod;
}
/*bool is_prime(ll x)//�����ж�
{
if (x == 2 || x == 3) return true;
if (x % 6 != 1 && x % 6 != 5) return false;
ll temp = sqrt(1.0*x);
for (ll i = 5;i <= temp;i += 6)
if (x%i == 0 || x % (i + 2) == 0) return false;
return true;
}
int prime[maxm], pnum = 0;//������pnum������
bool p[maxm] = { 0 };//�����ɸ������Ϊtrue
void Find_Prime()//�����ɸѡ��������һ���������Ͱ����ı���ȫ��ɸ��
{
for (int i = 2;i < maxm;i++)
{
if (p[i] == false)//����һ����һ�������ж�������
{
prime[pnum++] = i;
for (int j = i + i;j < maxm;j += i)//ɸ�������ı���
p[j] = true;
}
}
}

/*void Exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) x = 1, y = 0;
	else Exgcd(b, a % b, y, x), y -= a / b * x;
}//x��a��mod b�µ���Ԫ
ll CRT(int a[],int m[],int n)
{
ll M=1,ans=0,t,x,y;
for(int i=0; i<n; i++)
{
M*=m[i];///MΪ�����˻�
}
for(int i=0; i<n; i++)
{
t=M/m[i];///����mi�����n-1�������˻�
Exgcd(t,m[i],x,y);///����Ԫ������չŷ�����ת����t*ti+m[i]*y=1����ti
ans=(ans+a[i]*x*t)%M;
}
return (ans+M)%M;
}*/

ll a[maxn];
string s;
int n;
map<char, ll> m1;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> s;
	for (int i = 0;i < n;i++)
		cin >> a[i];
	for (int i = 0;i < n;i++)
	{
		if (s[i] == 'h')
			m1[s[i]] += a[i];
		else if (s[i] == 'a')
			m1[s[i]] = min(m1['h'], a[i]);
		else if (s[i] == 'r')
			m1[s[i]] = min(m1['a'], a[i]);
		else if (s[i] == 'd')
			m1[s[i]] = min(m1['r'], a[i]);
	}
	cout << m1['d'];
	return ~~(0 - 0);
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
#define lowbit(x) (x)&(-x)
typedef long long ll;
//dp[i]��ʾɾ������ǰ�����ַ�i����С�Ĵ��ۣ���ôdp[i]=min(dp[i-1],dp[i]+a[i])��ʾɾ����ǰ����ַ����Ż���ɾ���������ַ���

using namespace std;
const int maxn = 1e5+ 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9+7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll lcm(ll a, ll b)
{
return a / gcd(a, b)*b;
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	a = a%mod;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
	a = (a*a) % mod;
	}
	return ans;
}
/*ll C(ll n, ll m)//������ļ���
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = (ans*(n - m + i) / i)%mod;
	return ans%mod;
}
/*bool is_prime(ll x)//�����ж�
{
if (x == 2 || x == 3) return true;
if (x % 6 != 1 && x % 6 != 5) return false;
ll temp = sqrt(1.0*x);
for (ll i = 5;i <= temp;i += 6)
if (x%i == 0 || x % (i + 2) == 0) return false;
return true;
}
int prime[maxm], pnum = 0;//������pnum������
bool p[maxm] = { 0 };//�����ɸ������Ϊtrue
void Find_Prime()//�����ɸѡ��������һ���������Ͱ����ı���ȫ��ɸ��
{
for (int i = 2;i < maxm;i++)
{
if (p[i] == false)//����һ����һ�������ж�������
{
prime[pnum++] = i;
for (int j = i + i;j < maxm;j += i)//ɸ�������ı���
p[j] = true;
}
}
}

/*void Exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) x = 1, y = 0;
	else Exgcd(b, a % b, y, x), y -= a / b * x;
}//x��a��mod b�µ���Ԫ
ll CRT(int a[],int m[],int n)
{
ll M=1,ans=0,t,x,y;
for(int i=0; i<n; i++)
{
M*=m[i];///MΪ�����˻�
}
for(int i=0; i<n; i++)
{
t=M/m[i];///����mi�����n-1�������˻�
Exgcd(t,m[i],x,y);///����Ԫ������չŷ�����ת����t*ti+m[i]*y=1����ti
ans=(ans+a[i]*x*t)%M;
}
return (ans+M)%M;
}*/

ll a[maxn];
string s;
int n;
map<char, ll> m1, m2;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> s;
	for (int i = 0;i < n;i++)
		cin >> a[i];
	for (int i = 0;i < n;i++)
	{
		if (s[i] == 'h')
			m1[s[i]] += a[i];
		else if (s[i] == 'a')
			m1[s[i]] = min(m1['h'], m1[s[i]] + a[i]);
		else if (s[i] == 'r')
			m1[s[i]] = min(m1['a'], m1[s[i]] + a[i]);
		else if (s[i] == 'd')
			m1[s[i]] = min(m1['r'], m1[s[i]] + a[i]);
	}
	cout << m1['d'];
	return ~~(0 - 0);
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
#define lowbit(x) (x)&(-x)
typedef long long ll;
// ״̬�ǹ�����ǰ׺�ַ�����ת�Ʊ��� �����޷�����ǰi��ǰ׺����С���� ���ȿ��Կ��������޷�����ǰi-1���ַ�����С����
//Ȼ����ǿ��ǽ����λ�õ��ַ�ɾ�� �ù������� dp[i] �޷�����ǰi���ַ�����С����
using namespace std;
const int maxn = 1e5+ 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9+7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll lcm(ll a, ll b)
{
return a / gcd(a, b)*b;
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	a = a%mod;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
	a = (a*a) % mod;
	}
	return ans;
}
/*ll C(ll n, ll m)//������ļ���
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = (ans*(n - m + i) / i)%mod;
	return ans%mod;
}
/*bool is_prime(ll x)//�����ж�
{
if (x == 2 || x == 3) return true;
if (x % 6 != 1 && x % 6 != 5) return false;
ll temp = sqrt(1.0*x);
for (ll i = 5;i <= temp;i += 6)
if (x%i == 0 || x % (i + 2) == 0) return false;
return true;
}
int prime[maxm], pnum = 0;//������pnum������
bool p[maxm] = { 0 };//�����ɸ������Ϊtrue
void Find_Prime()//�����ɸѡ��������һ���������Ͱ����ı���ȫ��ɸ��
{
for (int i = 2;i < maxm;i++)
{
if (p[i] == false)//����һ����һ�������ж�������
{
prime[pnum++] = i;
for (int j = i + i;j < maxm;j += i)//ɸ�������ı���
p[j] = true;
}
}
}

/*void Exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) x = 1, y = 0;
	else Exgcd(b, a % b, y, x), y -= a / b * x;
}//x��a��mod b�µ���Ԫ
ll CRT(int a[],int m[],int n)
{
ll M=1,ans=0,t,x,y;
for(int i=0; i<n; i++)
{
M*=m[i];///MΪ�����˻�
}
for(int i=0; i<n; i++)
{
t=M/m[i];///����mi�����n-1�������˻�
Exgcd(t,m[i],x,y);///����Ԫ������չŷ�����ת����t*ti+m[i]*y=1����ti
ans=(ans+a[i]*x*t)%M;
}
return (ans+M)%M;
}*/

ll a[maxn];
string s;
int n;
map<char, ll> m1, m2;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> s;
	for (int i = 0;i < n;i++)
		cin >> a[i];
	for (int i = 0;i < n;i++)
	{
		if (s[i] == 'h')
			m1[s[i]] += a[i];
		else if (s[i] == 'a')
			m1[s[i]] = min(m1['h'], m1[s[i]] + a[i]);
		else if (s[i] == 'r')
			m1[s[i]] = min(m1['a'], m1[s[i]] + a[i]);
		else if (s[i] == 'd')
			m1[s[i]] = min(m1['r'], m1[s[i]] + a[i]);
	}
	cout << m2['d'];
	return ~~(0 - 0);
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
#define lowbit(x) (x)&(-x)
typedef long long ll;
//
using namespace std;
const int maxn = 1e5+ 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9+7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll lcm(ll a, ll b)
{
return a / gcd(a, b)*b;
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	a = a%mod;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
	a = (a*a) % mod;
	}
	return ans;
}
/*ll C(ll n, ll m)//������ļ���
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = (ans*(n - m + i) / i)%mod;
	return ans%mod;
}
/*bool is_prime(ll x)//�����ж�
{
if (x == 2 || x == 3) return true;
if (x % 6 != 1 && x % 6 != 5) return false;
ll temp = sqrt(1.0*x);
for (ll i = 5;i <= temp;i += 6)
if (x%i == 0 || x % (i + 2) == 0) return false;
return true;
}
int prime[maxm], pnum = 0;//������pnum������
bool p[maxm] = { 0 };//�����ɸ������Ϊtrue
void Find_Prime()//�����ɸѡ��������һ���������Ͱ����ı���ȫ��ɸ��
{
for (int i = 2;i < maxm;i++)
{
if (p[i] == false)//����һ����һ�������ж�������
{
prime[pnum++] = i;
for (int j = i + i;j < maxm;j += i)//ɸ�������ı���
p[j] = true;
}
}
}

/*void Exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) x = 1, y = 0;
	else Exgcd(b, a % b, y, x), y -= a / b * x;
}//x��a��mod b�µ���Ԫ
ll CRT(int a[],int m[],int n)
{
ll M=1,ans=0,t,x,y;
for(int i=0; i<n; i++)
{
M*=m[i];///MΪ�����˻�
}
for(int i=0; i<n; i++)
{
t=M/m[i];///����mi�����n-1�������˻�
Exgcd(t,m[i],x,y);///����Ԫ������չŷ�����ת����t*ti+m[i]*y=1����ti
ans=(ans+a[i]*x*t)%M;
}
return (ans+M)%M;
}*/

ll a[maxn];
string s;
int n;
map<char, ll> m1, m2;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> s;
	for (int i = 0;i < n;i++)
		cin >> a[i];
	for (int i = 0;i < n;i++)
	{
		if (s[i] == 'h')
		{
			m1[s[i]] += a[i];
			m2[s[i]] += a[i];
		}
		else if (s[i] == 'a')
		{
			m1[s[i]] = min(m2['h'], a[i]);
			m2[s[i]] += m1[s[i]];
		}
		else if (s[i] == 'r')
		{
			m1[s[i]] = min(m2['a'], a[i]);
			m2[s[i]] += m1[s[i]];
		}
		else if (s[i] == 'd')
		{
			m1[s[i]] = min(m2['r'], a[i]);
			m2[s[i]] += m1[s[i]];
		}
	}
	cout << min(m2['r'],min(m2['h'],min(m2['a'],m2['d'])));
	return ~~(0 - 0);
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
#define lowbit(x) (x)&(-x)
typedef long long ll;
//
using namespace std;
const int maxn = 1e5+ 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9+7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll lcm(ll a, ll b)
{
return a / gcd(a, b)*b;
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	a = a%mod;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
	a = (a*a) % mod;
	}
	return ans;
}
/*ll C(ll n, ll m)//������ļ���
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = (ans*(n - m + i) / i)%mod;
	return ans%mod;
}
/*bool is_prime(ll x)//�����ж�
{
if (x == 2 || x == 3) return true;
if (x % 6 != 1 && x % 6 != 5) return false;
ll temp = sqrt(1.0*x);
for (ll i = 5;i <= temp;i += 6)
if (x%i == 0 || x % (i + 2) == 0) return false;
return true;
}
int prime[maxm], pnum = 0;//������pnum������
bool p[maxm] = { 0 };//�����ɸ������Ϊtrue
void Find_Prime()//�����ɸѡ��������һ���������Ͱ����ı���ȫ��ɸ��
{
for (int i = 2;i < maxm;i++)
{
if (p[i] == false)//����һ����һ�������ж�������
{
prime[pnum++] = i;
for (int j = i + i;j < maxm;j += i)//ɸ�������ı���
p[j] = true;
}
}
}

/*void Exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) x = 1, y = 0;
	else Exgcd(b, a % b, y, x), y -= a / b * x;
}//x��a��mod b�µ���Ԫ
ll CRT(int a[],int m[],int n)
{
ll M=1,ans=0,t,x,y;
for(int i=0; i<n; i++)
{
M*=m[i];///MΪ�����˻�
}
for(int i=0; i<n; i++)
{
t=M/m[i];///����mi�����n-1�������˻�
Exgcd(t,m[i],x,y);///����Ԫ������չŷ�����ת����t*ti+m[i]*y=1����ti
ans=(ans+a[i]*x*t)%M;
}
return (ans+M)%M;
}*/

ll a[maxn];
string s;
int n;
map<char, ll> m1, m2;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> s;
	for (int i = 0;i < n;i++)
		cin >> a[i];
	for (int i = 0;i < n;i++)
	{
		if (s[i] == 'h')
		{
			m1[s[i]] += a[i];
			m2[s[i]] += a[i];
		}
		else if (s[i] == 'a')
		{
			m1[s[i]] = min(m2['h'], a[i]);
			m2[s[i]] += m1[s[i]];
		}
		else if (s[i] == 'r')
		{
			m1[s[i]] = min(m2['a'], a[i]);
			m2[s[i]] += m1[s[i]];
		}
		else if (s[i] == 'd')
		{
			m1[s[i]] = min(m2['r'], a[i]);
			m2[s[i]] += m1[s[i]];
		}
	}
	cout << m2['d'];
	return ~~(0 - 0);
}


#include<iostream>
#include<stack>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<ctime>
#include<math.h>
#include<functional>
#include<cstring>
#include<iomanip>
#include<set>
#include<utility>
#include<limits.h>
#include<stdio.h>
#define lowbit(x) (x)&(-x)
typedef long long ll;
//
using namespace std;
const int maxn = 1e5+ 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9+7;
const int inf = 0x3fffffff;

/*ll gcd(ll a, ll b)//���Լ��
{
	return b == 0 ? a : gcd(b, a%b);
}
ll lcm(ll a, ll b)
{
return a / gcd(a, b)*b;
}
ll fast_pow(ll a, ll k)//������
{
	ll ans = 1;
	a = a%mod;
	while (k != 0)
	{
		if (k & 1) ans = (ans*a) % mod;
		k >>= 1;
	a = (a*a) % mod;
	}
	return ans;
}
/*ll C(ll n, ll m)//������ļ���
{
	ll ans = 1;
	for (int i = 1;i <= m;i++)
		ans = (ans*(n - m + i) / i)%mod;
	return ans%mod;
}
/*bool is_prime(ll x)//�����ж�
{
if (x == 2 || x == 3) return true;
if (x % 6 != 1 && x % 6 != 5) return false;
ll temp = sqrt(1.0*x);
for (ll i = 5;i <= temp;i += 6)
if (x%i == 0 || x % (i + 2) == 0) return false;
return true;
}
int prime[maxm], pnum = 0;//������pnum������
bool p[maxm] = { 0 };//�����ɸ������Ϊtrue
void Find_Prime()//�����ɸѡ��������һ���������Ͱ����ı���ȫ��ɸ��
{
for (int i = 2;i < maxm;i++)
{
if (p[i] == false)//����һ����һ�������ж�������
{
prime[pnum++] = i;
for (int j = i + i;j < maxm;j += i)//ɸ�������ı���
p[j] = true;
}
}
}

/*void Exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) x = 1, y = 0;
	else Exgcd(b, a % b, y, x), y -= a / b * x;
}//x��a��mod b�µ���Ԫ
ll CRT(int a[],int m[],int n)
{
ll M=1,ans=0,t,x,y;
for(int i=0; i<n; i++)
{
M*=m[i];///MΪ�����˻�
}
for(int i=0; i<n; i++)
{
t=M/m[i];///����mi�����n-1�������˻�
Exgcd(t,m[i],x,y);///����Ԫ������չŷ�����ת����t*ti+m[i]*y=1����ti
ans=(ans+a[i]*x*t)%M;
}
return (ans+M)%M;
}*/

ll a[maxn];
string s;
int n;
map<char, ll> m1, m2;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> s;
	for (int i = 0;i < n;i++)
		cin >> a[i];
	for (int i = 0;i < n;i++)
	{
		if (s[i] == 'h')
		{
			m1[s[i]] += a[i];
			m2[s[i]] += a[i];
		}
		else if (s[i] == 'a')
		{
			m1[s[i]] = min(m2['h'], a[i]);
			m2[s[i]] += m1[s[i]];
		}
		else if (s[i] == 'r')
		{
			m1[s[i]] = min(m2['h'],min(m2['a'], a[i]));
			m2[s[i]] += m1[s[i]];
		}
		else if (s[i] == 'd')
		{
			m1[s[i]] = min(m2['r'],min(m2['h'], min(m2['a'], a[i])));
			m2[s[i]] += m1[s[i]];
		}
	}
	cout << m2['d'];
	return ~~(0 - 0);
}


# include <bits/stdc++.h>
typedef long long LL;
using namespace std;
LL c[1000001], a[1000001],H, A, R, D, suf[200005], t, T, o, l=1, u, mx;
vector <int> v[100001];
priority_queue <int> pq;
queue <int> q;
string s;
int main(){
	cin>>n>>s;
	for(int i=0; i<n; i++)
		cin>>a[i];

	for(int i=n-1; i>=0; i--){
		if(s[i]=='h')H+=min(a[i], A);
		if(s[i]=='a')A+=min(a[i], R);
		if(s[i]=='r')R+=min(a[i], D);
		if(s[i]=='d')D+=a[i];

	}
	cout<<H;
	return 0;
}


# include <bits/stdc++.h>
typedef long long LL;
using namespace std;
LL c[1000001], a[1000001],H, A, R, D, n, suf[200005], t, T, o, l=1, u, mx;
vector <int> v[100001];
priority_queue <int> pq;
queue <int> q;
string s;
int main(){
	cin>>n>>s;
	for(int i=0; i<n; i++)
		cin>>a[i];

	for(int i=n-1; i>=0; i--){
		if(s[i]=='h')H+=min(a[i], A);
		if(s[i]=='a')A+=min(a[i], R);
		if(s[i]=='r')R+=min(a[i], D);
		if(s[i]=='d')D+=a[i];

	}
	cout<<min(min(H, A), min(R, D));
	return 0;
}


# include <bits/stdc++.h>
typedef long long LL;
using namespace std;
LL c[1000001], a[1000001],H, A, R, D, n, suf[200005], t, T, o, l=1, u, mx;
vector <int> v[100001];
priority_queue <int> pq;
queue <int> q;
string s;
int main(){
	cin>>n>>s;
	for(int i=0; i<n; i++)
		cin>>a[i];

	for(int i=n-1; i>=0; i--){
		if(s[i]=='h')H+=min(a[i], A);
		if(s[i]=='a')A+=min(a[i], R);
		if(s[i]=='r')R+=min(a[i], D);
		if(s[i]=='d')D+=a[i];

	}
	cout<<H;
	return 0;
}


# include <bits/stdc++.h>
typedef long long LL;
using namespace std;
LL c[1000001], a[1000001],H, A, R, D, A1, R1, D1, n, suf[200005], t, T, o, l=1, u, mx;
vector <int> v[100001];
priority_queue <int> pq;
queue <int> q;
string s;
int main(){
	cin>>n>>s;
	for(int i=0; i<n; i++)
		cin>>a[i];

	for(int i=n-1; i>=0; i--){
		if(s[i]=='h')H+=min(a[i], A1), A1=A;
		if(s[i]=='a')A+=min(a[i], R1), R1=R;
		if(s[i]=='r')R+=min(a[i], D1), D1=D;
		if(s[i]=='d')D+=a[i];

	}
	cout<<min(min(H, A1), min(R1, D1));
	return 0;
}


# include <bits/stdc++.h>
typedef long long LL;
using namespace std;
LL c[1000001], a[1000001],H, A, R, D, n, suf[200005], t, T, o, l=1, u, mx;
vector <int> v[100001];
priority_queue <int> pq;
queue <int> q;
string s;
int main(){
	cin>>n>>s;
	for(int i=0; i<n; i++)
		cin>>a[i];

	for(int i=n-1; i>=0; i--){
		if(s[i]=='h')H=min(H+a[i], A);
		if(s[i]=='a')A=min(A+a[i], R);
		if(s[i]=='r')R=min(R+a[i], D);
		if(s[i]=='d')D=D+a[i];

	}
	cout<<min(min(H, A), min(R, D));
	return 0;
}


# include <bits/stdc++.h>
typedef long long LL;
using namespace std;
LL c[1000001], a[1000001],H, A, R, D, A1, R1, D1, n, suf[200005], t, T, o, l=1, u, mx;
vector <int> v[100001];
priority_queue <int> pq;
queue <int> q;
string s;
int main(){
	cin>>n>>s;
	for(int i=0; i<n; i++)
		cin>>a[i];

	for(int i=n-1; i>=0; i--){
		if(s[i]=='h')H+=min(a[i], A), D1=D, A1=A, R1=R;
		if(s[i]=='a')A+=min(a[i], R);
		if(s[i]=='r')R+=min(a[i], D);
		if(s[i]=='d')D+=a[i];

	}
	cout<<min(min(H, A1), min(R1, D1));
	return 0;
}


# include <bits/stdc++.h>
typedef long long LL;
using namespace std;
LL c[1000001], a[1000001],H, A, R, D, A1, R1, D1, n, suf[200005], t, T, o, l=1, u, mx;
vector <int> v[100001];
priority_queue <int> pq;
queue <int> q;
string s;
int main(){
	cin>>n>>s;
	for(int i=0; i<n; i++)
		cin>>a[i];

	for(int i=n-1; i>=0; i--){
		if(s[i]=='h')H+=min(a[i], A), A1=A;
		if(s[i]=='a')A+=min(a[i], R), R1=R;
		if(s[i]=='r')R+=min(a[i], D), D1=D;
		if(s[i]=='d')D+=a[i];

	}
	cout<<min(min(H, A1), min(R1, D1));
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define forn(a, i, n) for (int i = a; i < n; ++i)
#define all(x) x.begin(), x.end()

ll sqr(ll a) {
    return a * a;
}

const ll MOD = 998244353;
const ll INF = 1e16;

ll dp[100100][4];

int main(){
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);

    int n;
    string s;
    cin >> n;
    vector <int> v(n);
    cin >> s;
    forn(0, i, n) {
        cin >> v[i];
    }

    if (s[0] == 'h') dp[0][0] = v[0];

    forn(1, i, n){
        if (s[i] == 'h')
            dp[i][0] = dp[i - 1][0] + v[i];
        else
            dp[i][0] = dp[i - 1][0];
        if (s[i] == 'a')
            dp[i][1] = min(dp[i - 1][1] + v[i], dp[i - 1][0]);
        else
            dp[i][1] = dp[i - 1][1];
        if (s[i] == 'r')
            dp[i][2] = min(dp[i - 1][2] + v[i], dp[i - 1][1]);
        else
            dp[i][2] = dp[i - 1][2];
        if (s[i] == 'd')
            dp[i][3] = min(dp[i - 1][3] + v[i], dp[i - 1][2]);
        else
            dp[i][3] = dp[i - 1][3];
    }
    cout << dp[n - 1][3];

}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define forn(a, i, n) for (int i = a; i < n; ++i)
#define all(x) x.begin(), x.end()

ll sqr(ll a) {
    return a * a;
}

const ll MOD = 998244353;

map <char, ll> mp = {{'h', 1}, {'a', 2}, {'r', 3}, {'d', 4}};
map <char, ll> pm = {{'h', 4}, {'a', 3}, {'r', 2}, {'d', 1}};

int main(){
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
    int a[100100], b[100100];
    ll n;
    string s;
    cin >> n;
    cin >> s;
    vector <int> v(n);
    ll res[5];

    fill(a, a + n, 0); fill(b, b + n, 0);
    fill(res, res + 5, 0);
    forn(0, i, n){
        cin >> v[i];
    }
    for (int i = 0; i < n; ++i){
        if (mp.find(s[i]) != mp.end()) {
            bool ok = 1;
            for (int j = 1; j < mp[s[i]]; ++j)
                if (!res[j]) ok = 0;
            if (ok)
                res[mp[s[i]]] = 1;
        }
        for (int j = 1; j < 5; ++j){
            if (res[j]) a[i] = j;
        }
    }
    fill(res, res + 5, 0);
    for (int i = n - 1; i >= 0; --i){
        if (pm.find(s[i]) != pm.end()) {
            bool ok = 1;
            for (int j = 1; j < pm[s[i]]; ++j)
                if (!res[j]) ok = 0;
            if (ok)
                res[pm[s[i]]] = 1;
        }
        for (int j = 1; j < 5; ++j){
            if (res[j]) b[i] = j;
        }
    }
    fill(res, res + 4, 0);
    for (int i = 0; i < n; ++i){
        if (mp.find(s[i]) != mp.end()){
            if (a[i] >= mp[s[i]] && b[i] >= pm[s[i]]) res[mp[s[i]]] += v[i];
        }
    }
    cout << *min_element(res + 1, res + 5);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define forn(a, i, n) for (int i = a; i < n; ++i)
#define all(x) x.begin(), x.end()

ll sqr(ll a) {
    return a * a;
}

const ll MOD = 998244353;

map <char, int> mp = { {'h', 1}, {'a', 2}, {'r', 3}, {'d', 4} };
int main(){
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);

    int n;
    string s;
    cin >> n;
    vector <int> v(n);
    cin >> s;
    forn(0, i, n) {
        cin >> v[i];
    }
    ll dp[100][5];
    forn(0, i, n){
        fill(dp[i], dp[i] + 5, -1);
    }
    forn(0, j, 5){
        if (mp.find(s[0]) != mp.end()){
            if (j > mp[s[0]])
                dp[0][j] = -1;
            if (j == mp[s[0]])
                dp[0][j] = 0;
            if (j == 0)
                dp[0][j] = v[0];
        } else {
            if (j == 0)
                dp[0][j] = 0;
            else
                dp[0][j] = -1;
        }
    }
    forn(1, i, n){
        forn(0, j, 5){
            if (mp.find(s[i]) != mp.end()) {
                if (j == mp[s[i]]) {
                    dp[i][j] = dp[i - 1][j - 1];
                    if (dp[i - 1][j] != -1) dp[i][j] = min(dp[i][j], dp[i - 1][j]);
                }
                if (j < mp[s[i]]){
                    if (mp[s[i]] - j > 1)
                        dp[i][j] = dp[i - 1][j];
                    else {
                        if (dp[i - 1][j] != -1)
                            dp[i][j] = dp[i - 1][j] + v[i];
                        else
                            dp[i][j] = -1;
                    }
                }
                if (j > mp[s[i]]){
                    dp[i][j] = dp[i - 1][j];
                }
            } else {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }
    ll res = 1e16;
    forn(0, i, 4){
        if (dp[n - 1][i] != -1) res = min(res, dp[n - 1][i]);
    }
    cout << res;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define forn(a, i, n) for (int i = a; i < n; ++i)
#define all(x) x.begin(), x.end()

ll sqr(ll a) {
    return a * a;
}

const ll MOD = 998244353;

map <char, int> mp = { {'h', 1}, {'a', 2}, {'r', 3}, {'d', 4} };
int main(){
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);

    int n;
    string s;
    cin >> n;
    vector <int> v(n);
    cin >> s;
    forn(0, i, n) {
        cin >> v[i];
    }
    ll dp[100][5];
    forn(0, i, n){
        fill(dp[i], dp[i] + 5, -1);
    }
    forn(0, j, 5){
        if (mp.find(s[0]) != mp.end()){
            if (j > mp[s[0]])
                dp[0][j] = -1;
            if (j == mp[s[0]])
                dp[0][j] = 0;
            if (j == 0)
                dp[0][j] = v[0];
        } else {
            if (j == 0)
                dp[0][j] = 0;
            else
                dp[0][j] = -1;
        }
    }
    forn(1, i, n){
        forn(0, j, 5){
            if (mp.find(s[i]) != mp.end()) {
                if (j == mp[s[i]]){
                    dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1]);
                }
                if (j < mp[s[i]]){
                    if (mp[s[i]] - j > 1)
                        dp[i][j] = dp[i - 1][j];
                    else {
                        if (dp[i - 1][j] != -1)
                            dp[i][j] = dp[i - 1][j] + v[i];
                        else
                            dp[i][j] = -1;
                    }
                }
                if (j > mp[s[i]]){
                    dp[i][j] = dp[i - 1][j];
                }
            } else {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }
    ll res = 1e16;
    forn(0, i, 4){
        if (dp[n - 1][i] != -1) res = min(res, dp[n - 1][i]);
    }
    cout << res;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define forn(a, i, n) for (int i = a; i < n; ++i)
#define all(x) x.begin(), x.end()

ll sqr(ll a) {
    return a * a;
}

const ll MOD = 998244353;
const ll INF = 1e16;

ll dp[100100][5];
map <char, int> mp = { {'h', 1}, {'a', 2}, {'r', 3}, {'d', 4} };
int main(){
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);

    int n;
    string s;
    cin >> n;
    vector <int> v(n);
    cin >> s;
    forn(0, i, n) {
        cin >> v[i];
    }

    forn(0, i, n){
        fill(dp[i], dp[i] + 5, INF);
    }
    forn(0, j, 5){
        if (mp.find(s[0]) != mp.end()){
            if (j > mp[s[0]])
                dp[0][j] = INF;
            else if (j == mp[s[0]])
                dp[0][j] = 0;
            else if (j == 0)
                dp[0][j] = v[0];
            else if (j < mp[s[0]])
                dp[0][j] = INF;
        } else {
            if (j == 0)
                dp[0][j] = 0;
            else
                dp[0][j] = INF;
        }
    }
    forn(1, i, n){
        forn(0, j, 5){
            if (mp.find(s[i]) != mp.end()) {
                if (j == 0){
                    if (mp[s[i]] == 1)
                        dp[i][j] = dp[i - 1][j] + v[i];
                    else
                        dp[i][j] = dp[i - 1][j];
                }
                else if (j < mp[s[i]]){
                    if (mp[s[i]] - j == 1)
                        dp[i][j] = dp[i - 1][j] + v[i];
                    else
                     dp[i][j] = min(dp[i - 1][j], dp[i - 1][j]);
                }
                else if (j == mp[s[i]]) {
                    dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1]);
                }
                else if (j > mp[s[i]]){
                    dp[i][j] = dp[i - 1][j];
                }
            } else {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }
    ll res = 1e16;
    forn(0, i, 4){
        res = min(res, dp[n - 1][i]);
    }
    cout << res;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define forn(a, i, n) for (int i = a; i < n; ++i)
#define all(x) x.begin(), x.end()

ll sqr(ll a) {
    return a * a;
}

const ll MOD = 998244353;

map <char, ll> mp = {{'h', 1}, {'a', 2}, {'r', 3}, {'d', 4}};
map <char, ll> pm = {{'h', 4}, {'a', 3}, {'r', 2}, {'d', 1}};

int main(){
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
    int a[100100], b[100100];
    ll n;
    string s;
    cin >> n;
    cin >> s;
    vector <int> v(n);
    ll res[5];

    fill(a, a + n, 0); fill(b, b + n, 0);
    fill(res, res + 5, 0);
    forn(0, i, n){
        cin >> v[i];
    }
    for (int i = 0; i < n; ++i){
        if (mp.find(s[i]) != mp.end()) {
            bool ok = 1;
            for (int j = 1; j < mp[s[i]]; ++j)
                if (!res[j]) ok = 0;
            if (ok)
                res[mp[s[i]]] = 1;
        }
        for (int j = 1; j < 5; ++j){
            if (res[j]) a[i] = j;
        }
    }
    fill(res, res + 5, 0);
    for (int i = n - 1; i >= 0; --i){
        if (pm.find(s[i]) != pm.end()) {
            bool ok = 1;
            for (int j = 1; j < pm[s[i]]; ++j)
                if (!res[j]) ok = 0;
            if (ok)
                res[pm[s[i]]] = 1;
        }
        for (int j = 1; j < 5; ++j){
            if (res[j]) b[i] = j;
        }
    }
    fill(res, res + 4, 0);
    for (int i = 0; i < n; ++i){
        if (mp.find(s[i]) != mp.end()){
            if (a[i] >= mp[s[i]] && b[i] >= pm[s[i]]) res[mp[s[i]]] += (ll)v[i];
        }
    }
    cout << *min_element(res + 1, res + 5);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define forn(a, i, n) for (int i = a; i < n; ++i)
#define all(x) x.begin(), x.end()

ll sqr(ll a) {
    return a * a;
}

const ll MOD = 998244353;

map <char, ll> mp = {{'h', 1}, {'a', 2}, {'r', 3}, {'d', 4}};
map <char, ll> pm = {{'h', 4}, {'a', 3}, {'r', 2}, {'d', 1}};

int main(){
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
    int a[100100], b[100100];
    ll n;
    string s;
    cin >> n;
    cin >> s;
    vector <int> v(n);
    ll res[5];

    fill(a, a + n, 0); fill(b, b + n, 0);
    fill(res, res + 5, 0);
    forn(0, i, n){
        cin >> v[i];
    }
    for (int i = 0; i < n; ++i){
        if (mp.find(s[i]) != mp.end()) {
            bool ok = 1;
            for (int j = 1; j < mp[s[i]]; ++j)
                if (!res[j]) ok = 0;
            if (ok)
                res[mp[s[i]]] = 1;
        }
        for (int j = 1; j < 5; ++j){
            if (res[j]) a[i] = j;
        }
    }
    fill(res, res + 5, 0);
    for (int i = n - 1; i >= 0; --i){
        if (pm.find(s[i]) != pm.end()) {
            bool ok = 1;
            for (int j = 1; j < pm[s[i]]; ++j)
                if (!res[j]) ok = 0;
            if (ok)
                res[pm[s[i]]] = 1;
        }
        for (int j = 1; j < 5; ++j){
            if (res[j]) b[i] = j;
        }
    }
    fill(res, res + 5, 0);
    for (int i = 0; i < n; ++i){
        if (mp.find(s[i]) != mp.end()){
            if (a[i] >= mp[s[i]] && b[i] >= pm[s[i]]) res[mp[s[i]]] += (ll)v[i];
        }
    }
    cout << *min_element(res + 1, res + 5);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define forn(a, i, n) for (int i = a; i < n; ++i)
#define all(x) x.begin(), x.end()

ll sqr(ll a) {
    return a * a;
}

const ll MOD = 998244353;

ll dp[100100][5];
map <char, int> mp = { {'h', 1}, {'a', 2}, {'r', 3}, {'d', 4} };
int main(){
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);

    int n;
    string s;
    cin >> n;
    vector <int> v(n);
    cin >> s;
    forn(0, i, n) {
        cin >> v[i];
    }

    forn(0, i, n){
        fill(dp[i], dp[i] + 5, -1);
    }
    forn(0, j, 5){
        if (mp.find(s[0]) != mp.end()){
            if (j > mp[s[0]])
                dp[0][j] = -1;
            if (j == mp[s[0]])
                dp[0][j] = 0;
            if (j == 0)
                dp[0][j] = v[0];
        } else {
            if (j == 0)
                dp[0][j] = 0;
            else
                dp[0][j] = -1;
        }
    }
    forn(1, i, n){
        forn(0, j, 5){
            if (mp.find(s[i]) != mp.end()) {
                if (j == mp[s[i]]) {
                    if (dp[i - 1][j] == -1 && dp[i - 1][j - 1] == -1) dp[i][j] = -1;
                    else {
                        if (dp[i - 1][j - 1] != -1) dp[i][j] = dp[i - 1][j - 1];
                        if (dp[i - 1][j] != -1) {
                            if (dp[i][j] == -1) dp[i][j] = dp[i - 1][j];
                            else dp[i][j] = min(dp[i][j], dp[i - 1][j]);
                        }
                    }
                }
                if (j < mp[s[i]]){
                    if (mp[s[i]] - j > 1)
                        dp[i][j] = dp[i - 1][j];
                    else {
                        if (dp[i - 1][j] != -1)
                            dp[i][j] = dp[i - 1][j] + v[i];
                        else
                            dp[i][j] = -1;
                    }
                }
                if (j > mp[s[i]]){
                    dp[i][j] = dp[i - 1][j];
                }
            } else {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }
    ll res = 1e16;
    forn(0, i, 4){
        if (dp[n - 1][i] != -1) res = min(res, dp[n - 1][i]);
    }
    cout << res;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define forn(a, i, n) for (int i = a; i < n; ++i)
#define all(x) x.begin(), x.end()

ll sqr(ll a) {
    return a * a;
}

const ll MOD = 998244353;

ll dp[100100][5];
map <char, int> mp = { {'h', 1}, {'a', 2}, {'r', 3}, {'d', 4} };
int main(){
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);

    int n;
    string s;
    cin >> n;
    vector <int> v(n);
    cin >> s;
    forn(0, i, n) {
        cin >> v[i];
    }

    forn(0, i, n){
        fill(dp[i], dp[i] + 5, -1);
    }
    forn(0, j, 5){
        if (mp.find(s[0]) != mp.end()){
            if (j > mp[s[0]])
                dp[0][j] = -1;
            if (j == mp[s[0]])
                dp[0][j] = 0;
            if (j == 0)
                dp[0][j] = v[0];
        } else {
            if (j == 0)
                dp[0][j] = 0;
            else
                dp[0][j] = -1;
        }
    }
    forn(1, i, n){
        forn(0, j, 5){
            if (mp.find(s[i]) != mp.end()) {
                if (j == mp[s[i]]) {
                    dp[i][j] = dp[i - 1][j - 1];
                    if (dp[i - 1][j] != -1) dp[i][j] = min(dp[i][j], dp[i - 1][j]);
                }
                if (j < mp[s[i]]){
                    if (mp[s[i]] - j > 1)
                        dp[i][j] = dp[i - 1][j];
                    else {
                        if (dp[i - 1][j] != -1)
                            dp[i][j] = dp[i - 1][j] + v[i];
                        else
                            dp[i][j] = -1;
                    }
                }
                if (j > mp[s[i]]){
                    dp[i][j] = dp[i - 1][j];
                }
            } else {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }
    ll res = 1e16;
    forn(0, i, 4){
        if (dp[n - 1][i] != -1) res = min(res, dp[n - 1][i]);
    }
    cout << res;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define forn(a, i, n) for (int i = a; i < n; ++i)
#define all(x) x.begin(), x.end()

ll sqr(ll a) {
    return a * a;
}

const ll MOD = 998244353;

ll dp[100100][5];
map <char, int> mp = { {'h', 1}, {'a', 2}, {'r', 3}, {'d', 4} };
int main(){
    cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);

    int n;
    string s;
    cin >> n;
    vector <int> v(n);
    cin >> s;
    forn(0, i, n) {
        cin >> v[i];
    }

    forn(0, i, n){
        fill(dp[i], dp[i] + 5, -1);
    }
    forn(0, j, 5){
        if (mp.find(s[0]) != mp.end()){
            if (j > mp[s[0]])
                dp[0][j] = -1;
            else if (j == mp[s[0]])
                dp[0][j] = 0;
            else if (j == 0)
                dp[0][j] = v[0];
            else if (j < mp[s[0]])
                dp[0][j] = -1;
        } else {
            if (j == 0)
                dp[0][j] = 0;
            else
                dp[0][j] = -1;
        }
    }
    forn(1, i, n){
        forn(0, j, 5){
            if (mp.find(s[i]) != mp.end()) {
                if (j == mp[s[i]]) {
                    if (dp[i - 1][j] == -1 && dp[i - 1][j - 1] == -1) dp[i][j] = -1;
                    else {
                        if (dp[i - 1][j - 1] != -1) dp[i][j] = dp[i - 1][j - 1];
                        if (dp[i - 1][j] != -1) {
                            if (dp[i][j] == -1) dp[i][j] = dp[i - 1][j];
                            else dp[i][j] = min(dp[i][j], dp[i - 1][j]);
                        }
                    }
                }
                if (j < mp[s[i]]){
                    if (mp[s[i]] - j > 1)
                        dp[i][j] = dp[i - 1][j];
                    else {
                        if (dp[i - 1][j] != -1)
                            dp[i][j] = dp[i - 1][j] + v[i];
                        else
                            dp[i][j] = -1;
                    }
                }
                if (j > mp[s[i]]){
                    dp[i][j] = dp[i - 1][j];
                }
            } else {
                dp[i][j] = dp[i - 1][j];
            }
        }
    }
    ll res = 1e16;
    forn(0, i, 4){
        if (dp[n - 1][i] != -1) res = min(res, dp[n - 1][i]);
    }
    cout << res;
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define mod1 1000000000000000007
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>multiordered_set;
ll int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
vector<int> pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.push_back(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    if(a.first==b.first)return a.second<b.second;
    return a.first<b.first;
}
bool cmp(int x,int y){return x>y;}
ll int gcd(ll int a,ll int b)
{
    if(!a)return b;
    return gcd(b%a,a);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
    return true;
}
map<int,int>pa;
int find(int i){
    if(pa[i]==i)return i;
    return pa[i]=find(pa[i]);
}
void join(int i,int j){
    int a=find(i);
    int b=find(j);
    if(b>=a)pa[b]=a;
    else pa[a]=b;
}
int dx[]={-1,-1,-1,0,1,1,1,0};
int dy[]={-1,0,1,1,1,0,-1,-1};
int ddx[]={-1,0,1,0};
int ddy[]={0,-1,0,1};
string s;
int n;
ll h,a,r,d,x;
int main() {
    faltu;
    cin>>n>>s;
    for(int i=0;i<n;++i) {
        cin>>x;
        if(s[i]=='h')h+=x;
        if(s[i]=='a')a=min(h,a+x);
        if(s[i]=='r')r=min(a,r+x);
        if(s[i]=='d')d=min(r,d+x);
    }
    cout<<d;
    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define mod1 1000000000000000007
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>multiordered_set;
ll int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
vector<int> pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.push_back(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    if(a.first==b.first)return a.second<b.second;
    return a.first<b.first;
}
bool cmp(int x,int y){return x>y;}
ll int gcd(ll int a,ll int b)
{
    if(!a)return b;
    return gcd(b%a,a);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
    return true;
}
map<int,int>pa;
int find(int i){
    if(pa[i]==i)return i;
    return pa[i]=find(pa[i]);
}
void join(int i,int j){
    int a=find(i);
    int b=find(j);
    if(b>=a)pa[b]=a;
    else pa[a]=b;
}
int dx[]={-1,-1,-1,0,1,1,1,0};
int dy[]={-1,0,1,1,1,0,-1,-1};
int ddx[]={-1,0,1,0};
int ddy[]={0,-1,0,1};
int main()
{
    faltu;
    ll int n;
    cin>>n;
    string s;
    cin>>s;
    ll int val[n+1];
    vector<ll int>v[5];
    for(int i=0;i<n;i++)
    {
        cin>>val[i];
        if(s[i]=='h')v[0].push_back(i);
        else if(s[i]=='a')v[1].push_back(i);
        else if(s[i]=='r')v[2].push_back(i);
        else v[3].push_back(i);
    }
    ll int mn=mod1;
    for(int i=0;i<4;i++)
    {
        ll int ans=0;
        for(int j:v[i])
        {
            int flag=0;
            for(int k=i+1;k<4;k++)
            {
                if(v[k].size()==0||v[k][v[k].size()-1]<j)
                {
                    flag=1;
                    break;
                }
            }
            for(int k=0;k<i;k++)
            {
                if(v[k].size()==0||v[k][0]>j)
                {
                    flag=1;
                    break;
                }
            }
           // cout<<j<<" "<<flag<<endl;
            if(flag)break;
            ans+=val[j];
        }
        mn=min(mn,ans);
    }
    cout<<mn<<endl;
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define mod1 1000000000000000007
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>multiordered_set;
ll int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
vector<int> pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.push_back(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    if(a.first==b.first)return a.second<b.second;
    return a.first<b.first;
}
bool cmp(int x,int y){return x>y;}
ll int gcd(ll int a,ll int b)
{
    if(!a)return b;
    return gcd(b%a,a);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
    return true;
}
map<int,int>pa;
int find(int i){
    if(pa[i]==i)return i;
    return pa[i]=find(pa[i]);
}
void join(int i,int j){
    int a=find(i);
    int b=find(j);
    if(b>=a)pa[b]=a;
    else pa[a]=b;
}
int dx[]={-1,-1,-1,0,1,1,1,0};
int dy[]={-1,0,1,1,1,0,-1,-1};
int ddx[]={-1,0,1,0};
int ddy[]={0,-1,0,1};
int main()
{
    faltu;
    ll int n;
    cin>>n;
    string s;
    cin>>s;
    ll int val[n+1];
    vector<ll int>v[5];
    for(int i=0;i<n;i++)
    {
        cin>>val[i];
        if(s[i]=='h')v[0].push_back(i);
        else if(s[i]=='a')v[1].push_back(i);
        else if(s[i]=='r')v[2].push_back(i);
        else v[3].push_back(i);
    }
    ll int mn=mod1;
    for(int i=0;i<4;i++)
    {
        ll int ans=0;
        for(int j:v[i])
        {
            int flag=0;
            for(int k=i+1;k<4;k++)
            {
                if(v[k].size()==0||v[k][v[k].size()-1]<j)
                {
                    flag=1;
                    break;
                }
            }
            for(int k=0;k<i;k++)
            {
                if(v[k].size()||v[k][0]>j)
                {
                    flag=1;
                    break;
                }
            }
            if(flag)break;
            ans+=val[j];
        }
        mn=min(mn,ans);
    }
    cout<<mn<<endl;
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define mod1 1e18+7
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>multiordered_set;
ll int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
vector<int> pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.push_back(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    if(a.first==b.first)return a.second<b.second;
    return a.first<b.first;
}
bool cmp(int x,int y){return x>y;}
ll int gcd(ll int a,ll int b)
{
    if(!a)return b;
    return gcd(b%a,a);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
    return true;
}
map<int,int>pa;
int find(int i){
    if(pa[i]==i)return i;
    return pa[i]=find(pa[i]);
}
void join(int i,int j){
    int a=find(i);
    int b=find(j);
    if(b>=a)pa[b]=a;
    else pa[a]=b;
}
int dx[]={-1,-1,-1,0,1,1,1,0};
int dy[]={-1,0,1,1,1,0,-1,-1};
int ddx[]={-1,0,1,0};
int ddy[]={0,-1,0,1};
const int maxn=3e5+10;
const long long big=1e18;
long long dp[maxn][5];
int n,a[maxn];
int main()
{
    faltu;
    char fuck[5];
    string s;
    fuck[0]='h',fuck[1]='a',fuck[2]='r',fuck[3]='d';
    for(int i=0;i<=int(1e5+10);i++)
        for(int j=0;j<=4;j++)
        dp[i][j]=big;
    cin>>n>>s;
    dp[0][0]=0;
    for(int i=0;i<n;i++)
        cin>>a[i];
    for(int i=0;i<n;i++){
        for(int j=0;j<4;j++){
            if(s[i]==fuck[j]){
            dp[i+1][j+1]=min(dp[i][j],dp[i+1][j+1]);
            dp[i+1][j]=min(dp[i+1][j],dp[i][j]+a[i]);
            }
            else
            dp[i+1][j]=min(dp[i+1][j],dp[i][j]);
        }
    }
    long long ans=big;
    for(int i=0;i<4;i++)
        ans=min(ans,dp[n][i]);
    cout<<ans<<endl;
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define mod1 1000000000000000007
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>multiordered_set;
ll int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
vector<int> pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.push_back(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    if(a.first==b.first)return a.second<b.second;
    return a.first<b.first;
}
bool cmp(int x,int y){return x>y;}
ll int gcd(ll int a,ll int b)
{
    if(!a)return b;
    return gcd(b%a,a);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
    return true;
}
map<int,int>pa;
int find(int i){
    if(pa[i]==i)return i;
    return pa[i]=find(pa[i]);
}
void join(int i,int j){
    int a=find(i);
    int b=find(j);
    if(b>=a)pa[b]=a;
    else pa[a]=b;
}
int dx[]={-1,-1,-1,0,1,1,1,0};
int dy[]={-1,0,1,1,1,0,-1,-1};
int ddx[]={-1,0,1,0};
int ddy[]={0,-1,0,1};
int main()
{
    faltu;
    ll int n;
    cin>>n;
    string s;
    cin>>s;
    ll int val[n+1];
    vector<ll int>v[5];
    for(int i=0;i<n;i++)
    {
        cin>>val[i];
        if(s[i]=='h')v[0].push_back(i);
        else if(s[i]=='a')v[1].push_back(i);
        else if(s[i]=='r')v[2].push_back(i);
        else v[3].push_back(i);
    }
    ll int mn=mod1;
    for(int i=0;i<4;i++)
    {
        ll int ans=0;
        for(int j:v[i])
        {
            int flag=0;
            for(int k=i+1;k<4;k++)
            {
                if(v[k].size()==0||v[k][v[k].size()-1]<j)
                {
                    flag=1;
                    break;
                }
            }
            if(flag)break;
            ans+=val[j];
        }
        mn=min(mn,ans);
    }
    cout<<mn<<endl;
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define faltu ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define mod1 1000000000000000007
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>multiordered_set;
ll int pw(ll int a, ll int b) {
    if (b == 0) return 1;
    ll int t = pw(a, b / 2);
    if (b % 2 == 0) return (t * t)%mod;
    else return ((t * t)%mod * a)%mod;
}
vector<int> pm;
void prm(int n)
{
    bool prime[n+1];
    memset(prime, true, sizeof(prime));
    for (int p=2; p*p<=n; p++)
    {
        if (prime[p] == true)
        {
            for (int i=p*p; i<=n; i += p)
              prime[i] = false;
        }
    }
    for (int p=2; p<=n; p++)
       if (prime[p]) pm.push_back(p);
}
bool sortbysecdesc(const pair<ll int,ll int> &a,const pair<ll int,ll int> &b)
{
    if(a.first==b.first)return a.second<b.second;
    return a.first<b.first;
}
bool cmp(int x,int y){return x>y;}
ll int gcd(ll int a,ll int b)
{
    if(!a)return b;
    return gcd(b%a,a);
}
bool isPrime(int n)
{
    if (n <= 1)
        return false;
    for (int i = 2; i < n; i++)
        if (n % i == 0)
            return false;
    return true;
}
map<int,int>pa;
int find(int i){
    if(pa[i]==i)return i;
    return pa[i]=find(pa[i]);
}
void join(int i,int j){
    int a=find(i);
    int b=find(j);
    if(b>=a)pa[b]=a;
    else pa[a]=b;
}
int dx[]={-1,-1,-1,0,1,1,1,0};
int dy[]={-1,0,1,1,1,0,-1,-1};
int ddx[]={-1,0,1,0};
int ddy[]={0,-1,0,1};
int main()
{
    faltu;
    ll int n;
    cin>>n;
    string s;
    cin>>s;
    ll int val[n+1];
    vector<ll int>v[5];
    for(int i=0;i<n;i++)
    {
        cin>>val[i];
        if(s[i]=='h')v[0].push_back(i);
        else if(s[i]=='a')v[1].push_back(i);
        else if(s[i]=='r')v[2].push_back(i);
        else if(s[i]=='d')v[3].push_back(i);
    }
    ll int mn=mod1;
    for(int i=0;i<4;i++)
    {
        ll int ans=0;
        for(int j:v[i])
        {
            int flag=0;
            for(int k=i+1;k<4;k++)
            {
                if(v[k].size()==0||v[k][v[k].size()-1]<j)
                {
                    flag=1;
                    break;
                }
            }
            for(int k=0;k<i;k++)
            {
                if(v[k].size()==0||v[k][0]>j)
                {
                    flag=1;
                    break;
                }
            }
          //  cout<<j<<" "<<flag<<endl;
            if(flag)break;
            ans+=val[j];
        }
        mn=min(mn,ans);
    }
    cout<<mn<<endl;
}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 998244354
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

int n;
ll dp[100010][5];
vll a(100010);
string s;
string t="xhard";

void solve()
{
  for(int i=1;i<n;i++) for(int j=1;j<5;j++)
  {
	  if(s[i]==t[j])
	  {
		  if(dp[i][j]!=inf) dp[i][j]+=a[i];
		  else dp[i][j]=min(dp[i-1][j-1],a[i]);

		  dp[i+1][j]=dp[i][j];
	  }
	  else
	  	dp[i][j]=dp[i-1][j];

  }
  cout <<(dp[n-1][4]==inf?0:dp[n-1][4])<<endl;
}
int main()
{
	for(int i=0;i<=int(1e5)+5;i++) for(int j=0;j<5;j++) dp[i][j]=inf;
  cin >> n;n++;
  cin >> s;
  s.insert(s.begin(),'x');
  for(int i=1;i<n;i++) cin >> a[i];

  solve();

}


#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define endl "\n"
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 998244354
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define f first
#define s second

using namespace std;

int n;
ll dp[100010][5];
vll a(100010);
string s;
string t="xhard";

void solve()
{
  for(int i=1;i<n;i++) for(int j=1;j<5;j++)
  {
    if(s[i]==t[j])
    {
      if(dp[i-1][j]!=inf)
        dp[i][j]=dp[i-1][j]+a[i];
      else
        dp[i][j]=min(dp[i-1][j-1],a[i]);
    }
    else
      dp[i][j]=(dp[i-1][j]);
  }
  cout << (dp[n-1][4]==inf?0:dp[n-1][4])<<endl;
}
int main()
{
	for(int i=0;i<=int(1e5)+5;i++) for(int j=0;j<5;j++) dp[i][j]=inf;
  cin >> n;n++;
  cin >> s;
  s.insert(s.begin(),'x');
  for(int i=1;i<n;i++) cin >> a[i];

  solve();

}