Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length $n$ consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaard can be considered hard statements, while har, hart and drah are easy statements. 

Vasya doesn't want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is $0$, and removing $i$-th character increases the ambiguity by $a_i$ (the index of each character is considered as it was in the original statement, so, for example, if you delete character r from hard, and then character d, the index of d is still $4$ even though you delete it from the string had).

Vasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it!

Recall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.


-----Input-----

The first line contains one integer $n$ ($1 \le n \le 10^5$) — the length of the statement.

The second line contains one string $s$ of length $n$, consisting of lowercase Latin letters — the statement written by Vasya.

The third line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 998244353$).


-----Output-----

Print minimum possible ambiguity of the statement after Vasya deletes some (possibly zero) characters so the resulting statement is easy.


-----Examples-----
Input
6
hhardh
3 2 9 11 7 1

Output
5

Input
8
hhzarwde
3 2 6 9 4 8 7 1

Output
4

Input
6
hhaarr
1 2 3 4 5 6

Output
0



-----Note-----

In the first example, first two characters are removed so the result is ardh.

In the second example, $5$-th character is removed so the result is hhzawde.

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

Difficulty: MEDIUM_HARD

Solutions:
R = lambda : map(int, input().split())
n = int(input())
s = input()
cs = list(R())
c2c = {c: i for (i, c) in enumerate('hard')}
dp = [0] * 4 + [10 ** 19]
for (i, c) in enumerate(s):
	if c in 'hard':
		idx = c2c[c]
		dp[idx] = min(dp[idx] + cs[i], dp[idx - 1])
print(min(dp))


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
s = input()
b = list(map(int, input().split()))
(h, a, r, d) = (0, 0, 0, 0)
for i in range(n):
	if s[i] == 'h':
		h += b[i]
	elif s[i] == 'a':
		a = min(a + b[i], h)
	elif s[i] == 'r':
		r = min(r + b[i], a)
	elif s[i] == 'd':
		d = min(d + b[i], r)
print(d)


n = int(input())
s = input()
a = list(map(int, input().split()))
hard = {'h': 1, 'a': 2, 'r': 3, 'd': 4}
cost = [10 ** 20, 0, 0, 0, 0]
for (ai, c) in zip(a, s):
	if c in hard:
		cost[hard[c]] = min(cost[hard[c] - 1], cost[hard[c]] + ai)
print(cost[4])


n = int(input())
s = input()
b = list(map(int, input().split()))
h = [0] * n
ha = [0] * n
har = [0] * n
hard = [0] * n
if s[0] == 'h':
	h[0] = b[0]
for j in range(1, n):
	if s[j] == 'h':
		h[j] = h[j - 1] + b[j]
		ha[j] = ha[j - 1]
		har[j] = har[j - 1]
		hard[j] = hard[j - 1]
	elif s[j] == 'a':
		ha[j] = min(ha[j - 1] + b[j], h[j - 1])
		h[j] = h[j - 1]
		har[j] = har[j - 1]
		hard[j] = hard[j - 1]
	elif s[j] == 'r':
		har[j] = min(har[j - 1] + b[j], h[j - 1], ha[j - 1])
		h[j] = h[j - 1]
		ha[j] = ha[j - 1]
		hard[j] = hard[j - 1]
	elif s[j] == 'd':
		hard[j] = min(hard[j - 1] + b[j], har[j - 1], ha[j - 1], h[j - 1])
		h[j] = h[j - 1]
		ha[j] = ha[j - 1]
		har[j] = har[j - 1]
	else:
		h[j] = h[j - 1]
		ha[j] = ha[j - 1]
		har[j] = har[j - 1]
		hard[j] = hard[j - 1]
print(hard[-1])


n = int(input())
s = input().strip()
a = list(map(int, input().split()))
t = 'hard'
m = len(t)
dp = [[-1] * m for i in range(n)]
dp[n - 1][m - 1] = a[n - 1] if s[n - 1] == t[m - 1] else 0
for i in reversed(range(n - 1)):
	dp[i][m - 1] = dp[i + 1][m - 1] + (a[i] if s[i] == t[m - 1] else 0)
for i in reversed(range(n)):
	for j in reversed(range(m - 1)):
		if n - i < m - j:
			dp[i][j] = 0
		else:
			ans = -1
			if s[i] != t[j]:
				ans = dp[i + 1][j]
			else:
				ans = min(dp[i + 1][j + 1], dp[i + 1][j] + a[i])
			dp[i][j] = ans
print(dp[0][0])


import math
h = 'hard'
n = int(input())
s = input()
a = list(map(int, input().split()))
dp = [[math.inf for _ in range(5)] for _ in range(n + 1)]
dp[0][0] = 0
for i in range(0, n):
	for j in range(0, 4):
		if s[i] == h[j]:
			dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j])
		else:
			dp[i + 1][j] = min(dp[i + 1][j], dp[i][j])
		dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i])
print(min(dp[n][:4]))


_ = input()
s = input()
a = list(map(int, input().split()))
hard = 'hard'
dp = [0] * 4
for i in range(len(s)):
	if s[i] == hard[0]:
		dp[0] += a[i]
	for j in range(1, 4):
		if s[i] == hard[j]:
			dp[j] = min(dp[j - 1], dp[j] + a[i])
print(min(dp))


import sys

def input():
	return sys.stdin.readline().rstrip()

def to_int(char):
	return ord(char) - ord('a')
hard = list(map(to_int, list('hard')))
INF = 1 << 64

def slv():
	n = int(input())
	a = list(map(to_int, list(input())))
	cost = list(map(int, input().split()))
	dp = [[INF] * 5 for i in range(n + 1)]
	dp[0][0] = 0
	for (i, v) in enumerate(a):
		for subseq in range(0, 4):
			if v == hard[subseq]:
				dp[i + 1][subseq + 1] = min(dp[i + 1][subseq + 1], dp[i][subseq])
				dp[i + 1][subseq] = min(dp[i + 1][subseq], dp[i][subseq] + cost[i])
			else:
				dp[i + 1][subseq] = min(dp[i + 1][subseq], dp[i][subseq])
	ans = min((dp[n][subseq] for subseq in range(4)))
	print(ans)
	return

def main():
	t = 1
	for i in range(t):
		slv()
	return
main()


from bisect import *
n = int(input())
s = list(input())
a = [int(x) for x in input().split()]
f = 'hard'
dp = [[10 ** 18, 10 ** 18, 10 ** 18, 10 ** 18, 10 ** 18] for i in range(n + 1)]
dp[0] = [10 ** 18, 0, 0, 0, 0]
for i in range(1, n + 1):
	for j in range(1, 5):
		if s[i - 1] == f[j - 1]:
			dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j] + a[i - 1])
		else:
			dp[i][j] = min(dp[i][j], dp[i - 1][j])
print(min(dp[n]))


import sys

def main():
	n = int(sys.stdin.readline())
	s = sys.stdin.readline()
	cost = list(map(int, sys.stdin.readline().split()))
	(no_h, no_ha, no_har, no_hard) = (0, 0, 0, 0)
	for i in range(n):
		if s[i] == 'h':
			no_h += cost[i]
		if s[i] == 'a':
			no_ha = min(no_ha + cost[i], no_h)
		if s[i] == 'r':
			no_har = min(no_har + cost[i], no_ha)
		if s[i] == 'd':
			no_hard = min(no_hard + cost[i], no_har)
	print(no_hard)
main()


import sys

def eprint(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)
n = int(input())
s = input()
ambiguity = list(map(int, input().split()))
s_a = [y for y in list(zip([x for x in s], ambiguity)) if y[0] in 'hard']
if len(s_a) < 4:
	print(0)
	quit()
l = len(s_a) + 1
h = [0] * l
ha = [0] * l
har = [0] * l
hard = [0] * l
last_a = -1
last_r = -1
last_d = -1
for (i, (c, a)) in enumerate(s_a):
	if c == 'h':
		h[i] = a + h[i - 1]
	else:
		h[i] = h[i - 1]
	if c == 'a':
		ha[i] = min(h[i], a + ha[last_a])
		last_a = i
	else:
		ha[i] = ha[i - 1]
	if c == 'r':
		har[i] = min(ha[i], a + har[last_r])
		last_r = i
	else:
		har[i] = har[i - 1]
	if c == 'd':
		hard[i] = min(har[i], a + hard[last_d])
		last_d = i
	else:
		hard[i] = hard[i - 1]
eprint(h)
eprint(ha)
eprint(har)
eprint(hard)
print(hard[-2])


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)

def getIntList():
	return list(map(int, input().split()))
try:
	import numpy

	def dprint(*args, **kwargs):
		print(*args, file=sys.stderr)
	dprint('debug mode')
except Exception:

	def dprint(*args, **kwargs):
		pass
inId = 0
outId = 0
if inId > 0:
	dprint('use input', inId)
	sys.stdin = open('input' + str(inId) + '.txt', 'r')
if outId > 0:
	dprint('use output', outId)
	sys.stdout = open('stdout' + str(outId) + '.txt', 'w')
	atexit.register(lambda : sys.stdout.close())
(N,) = getIntList()
S = input()
za = getIntList()
bbb = 10 ** 20
dp = [0, bbb, bbb, bbb]
for i in range(N):
	dp1 = dp.copy()
	c = S[i]
	w = za[i]
	if c == 'h':
		dp1[1] = min(dp1[1], dp1[0])
		dp1[0] += w
	elif c == 'a':
		dp1[2] = min(dp1[2], dp1[1])
		dp1[1] += w
	elif c == 'r':
		dp1[3] = min(dp1[3], dp1[2])
		dp1[2] += w
	elif c == 'd':
		dp1[3] += w
	dp = dp1
print(min(dp))


n = int(input())
s = input()
a = list(map(int, input().split()))
pat = 'hard'
dp = [[0] * (n + 1) for _ in range(4)]
for i in range(1, n + 1):
	dp[0][i] = dp[0][i - 1] + (0 if s[i - 1] != 'h' else a[i - 1])
for i in range(1, 4):
	for j in range(1, n + 1):
		if pat[i] == s[j - 1]:
			dp[i][j] = min(dp[i - 1][j - 1], dp[i][j - 1] + a[j - 1])
		else:
			dp[i][j] = dp[i][j - 1]
print(dp[-1][-1])


import sys
import math as mt
t = 1
for ___ in range(t):
	n = int(input())
	s = input()
	s1 = 'hard'
	l = list(map(int, input().split()))
	dp = [[10 ** 18 + 1 for j in range(5)] for i in range(len(s) + 1)]
	index = [0] * (n + 1)
	for i in range(1, 5):
		dp[0][i] = 0
	d1 = {}
	ch = 0
	for i in range(1, len(s) + 1):
		for j in range(1, 5):
			if s[i - 1] == s1[j - 1]:
				dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j] + l[i - 1])
			else:
				dp[i][j] = dp[i - 1][j]
	ans = 0
	print(dp[len(s)][4])


l = int(input())
s = input()
arr = list(map(int, input().split()))
hard = 'hard'
dp = [0] * 4
for i in range(l):
	if s[i] == hard[0]:
		dp[0] += arr[i]
	for j in range(1, 4):
		if s[i] == hard[j]:
			dp[j] = min(dp[j - 1], dp[j] + arr[i])
print(min(dp))


import sys, os, io
from sys import stdin
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
import math
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def si():
	return input()

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
t = 1
for _ in range(t):
	n = ii()
	s = list(input())
	l = li()
	hard = 'hard'
	ptr = 0
	for i in s:
		if i == hard[ptr]:
			ptr += 1
		if ptr == 4:
			break
	if ptr < 4:
		print(0)
		continue
	dp = [[100000000000000000 for i in range(4)] for j in range(n + 1)]
	for i in range(n + 1):
		for j in range(4):
			if i == 0:
				dp[i] = [0, 0, 0, 0]
				continue
			if s[i - 1] != hard[j]:
				dp[i][j] = dp[i - 1][j]
			else:
				if j == 0:
					dp[i][j] = dp[i - 1][j] + l[i - 1]
					continue
				dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j] + l[i - 1])
	print(min(dp[-1]))


import bisect
from math import inf

def naive(chars, start, start_t, values, word):
	t = start_t
	i = start
	indexes = []
	while t < len(word) and i < len(chars):
		if chars[i] == word[t]:
			t += 1
			indexes.append(i)
		i += 1
	if t == len(word):
		min_cost = inf
		for (p, j) in enumerate(indexes):
			cost = values[j] + naive(chars, j + 1, p + start_t, values, word)
			if min_cost > cost:
				min_cost = cost
		return min_cost
	else:
		return 0

def naive_mem(chars, start, start_t, values, word, mem):
	if start >= len(chars):
		return 0
	if mem[start][start_t] != -1:
		return mem[start][start_t]
	t = start_t
	i = start
	indexes = []
	while t < len(word) and i < len(chars):
		if chars[i] == word[t]:
			t += 1
			indexes.append(i)
		i += 1
	if t == len(word):
		min_cost = inf
		for (p, j) in enumerate(indexes):
			cost_foo = naive_mem(chars, j + 1, p + start_t, values, word, mem)
			cost = values[j] + cost_foo
			if min_cost > cost:
				min_cost = cost
		mem[start][start_t] = min_cost
		return min_cost
	else:
		mem[start][start_t] = 0
		return 0

def find_word_reverse(chars, word):
	t = 0
	i = len(chars) - 1
	indexes = []
	while t < len(word) and i >= 0:
		if chars[i] == word[len(word) - t - 1]:
			t += 1
			indexes.insert(0, i)
		i -= 1
	return indexes

def find_word(chars, word):
	t = 0
	i = 0
	indexes = []
	while t < len(word) and i < len(chars):
		if chars[i] == word[t]:
			t += 1
			indexes.append(i)
		i += 1
	return indexes

def clean(chars, values):
	new_chars = []
	new_values = []
	last_c = ''
	cum_value = 0
	i = 0
	while i < len(chars):
		if last_c != '' and chars[i] != last_c:
			new_chars.append(last_c)
			new_values.append(cum_value)
			cum_value = 0
		last_c = chars[i]
		cum_value += values[i]
		i += 1
	new_chars.append(last_c)
	new_values.append(cum_value)
	return (new_chars, new_values)

def naive_tab(chars, values, word):
	last_indexes = find_word_reverse(chars, word)
	first_indexes = find_word(chars, word)
	mem = [[0 for _ in range(4)] for _ in range(len(chars) + 1)]
	positions = [[] for _ in range(4)]
	for p in range(4):
		positions[p].append(len(mem) - 1)
	last_costs = [0, 0, 0, 0]
	if len(first_indexes) != len(word):
		return 0
	for i in range(len(chars) - 1, -1, -1):
		c = chars[i]
		word_i = 0
		try:
			word_i = word.index(c)
		except ValueError:
			continue
		if i < first_indexes[word_i] or i > last_indexes[word_i]:
			continue
		min_cost = mem[positions[word_i][0]][word_i]
		next_w = positions[word_i][0]
		for x in range(word_i + 1, len(word)):
			bis = bisect.bisect_right(positions[x], next_w)
			if len(positions[x]) <= bis:
				break
			ind = positions[x][bis]
			next_w = ind
			cost = mem[ind][x]
			if min_cost > cost:
				min_cost = cost
		min_cost += values[i]
		mem[i][word_i] = min_cost
		last_costs[word_i] = min_cost
		positions[word_i].insert(0, i)
	return min(last_costs)
n = int(input())
s = input()
values = list(map(int, input().split()))
chars = [c for c in s]
mem = [[-1, -1, -1, -1] for _ in range(len(s))]
tple = clean(chars, values)
print(naive_tab(tple[0], tple[1], [c for c in 'hard']))


a = int(input())
b = input()
c = list(map(int, input().split()))
d = {'h': 1, 'a': 2, 'r': 3, 'd': 4}
e = [10 ** 20, 0, 0, 0, 0]
for (f, g) in zip(c, b):
	if g in d:
		e[d[g]] = min(e[d[g] - 1], e[d[g]] + f)
print(e[4])


import sys
from array import array
import typing as Tp

def input():
	return sys.stdin.buffer.readline().decode('utf-8')

def main():
	n = int(input())
	s = input().rstrip()
	a = tuple(map(int, input().split()))
	dp = [0] * 4
	hard = 'hard'
	for (char, cost) in zip(s, a):
		dp = [dp[j] if char != hard[j] else min(dp[j - 1], dp[j] + cost) if j > 0 else dp[j] + cost for j in range(4)]
	print(min(dp))
main()


n = int(input())
a = input()
w = list(map(int, input().split()))
dp = [[float('inf')] * 4 for _ in range(n)]
(hl, al, rl, dl) = (n, n, n, n)
(dp[-1][0], dp[-1][1], dp[-1][2], dp[-1][3]) = (0, 0, 0, 0)
if a[-1] == 'd':
	dp[-1][3] = w[-1]
for i in range(n - 2, -1, -1):
	ch = a[i]
	delta = 0
	if ch == 'h':
		if hl < n:
			delta = dp[hl][0]
		dp[i][0] = min(w[i] + delta, dp[i + 1][1])
		(dp[i][1], dp[i][2], dp[i][3]) = (dp[i + 1][1], dp[i + 1][2], dp[i + 1][3])
		hl = i
	elif ch == 'a':
		if al < n:
			delta = dp[al][1]
		dp[i][1] = min(w[i] + delta, dp[i + 1][2])
		(dp[i][0], dp[i][2], dp[i][3]) = (dp[i + 1][0], dp[i + 1][2], dp[i + 1][3])
		al = i
	elif ch == 'r':
		if rl < n:
			delta = dp[rl][2]
		dp[i][2] = min(w[i] + delta, dp[i + 1][3])
		(dp[i][0], dp[i][1], dp[i][3]) = (dp[i + 1][0], dp[i + 1][1], dp[i + 1][3])
		rl = i
	elif ch == 'd':
		dp[i][3] = w[i] + dp[i + 1][3]
		(dp[i][0], dp[i][1], dp[i][2]) = (dp[i + 1][0], dp[i + 1][1], dp[i + 1][2])
		dl = i
	else:
		(dp[i][0], dp[i][1], dp[i][2], dp[i][3]) = (dp[i + 1][0], dp[i + 1][1], dp[i + 1][2], dp[i + 1][3])
print(dp[0][0])


import sys
W = 'hard'
INF = 123456

def solve(io):
	N = io.read_int()
	S = io.read_line()
	A = io.read_int_array(N)
	dp = [[INF] * len(W) for i in range(0, N + 1)]
	dp[-1][0] = 0
	for i in range(0, N):
		for j in range(0, len(W)):
			dp[i][j] = dp[i - 1][j]
			if S[i] == W[j]:
				dp[i][j] += A[i]
		for j in range(0, len(W) - 1):
			if S[i] == W[j]:
				dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j])
	io.println(min(dp[N - 1]))

class IO:
	in_stream = None
	out_stream = None
	raw = ''
	buf = []
	pos = 0

	def __init__(self, input_stream, output_stream):
		self.in_stream = input_stream
		self.out_stream = output_stream

	def read_to_buffer(self):
		self.raw = self.in_stream.readline().rstrip('\n')
		self.buf = self.raw.split()
		self.pos = 0

	def read_string(self):
		while self.pos == len(self.buf):
			self.read_to_buffer()
		ans = self.buf[self.pos]
		self.pos += 1
		return ans

	def read_int(self):
		return int(self.read_string())

	def read_float(self):
		return float(self.read_string())

	def read_string_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_string())
		return arr

	def read_int_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_int())
		return arr

	def read_float_array(self, N, offset=0):
		arr = [None] * offset
		for _ in range(0, N):
			arr.append(self.read_float())
		return arr

	def read_line(self):
		while self.pos == len(self.buf):
			self.read_to_buffer()
		if self.pos > 0:
			raise ValueError('Cannot call read_line in the middle of a line.')
		self.pos = len(self.buf)
		return self.raw

	def print(self, *args):
		self.out_stream.write(' '.join([str(x) for x in args]))

	def println(self, *args):
		self.print(*args)
		self.print('\n')

	def println_array(self, arr, sep=' '):
		self.println(sep.join((str(x) for x in arr)))

	def flush_output(self):
		self.out_stream.flush()
pythonIO = IO(sys.stdin, sys.stdout)
solve(pythonIO)
pythonIO.flush_output()


import sys
input = sys.stdin.readline
n = int(input())
s = input()
a = list(map(int, input().split()))
dp = [0] * 4
for i in range(n):
	if s[i] == 'h':
		dp[0] += a[i]
	elif s[i] == 'a':
		dp[1] = min(dp[0], a[i] + dp[1])
	elif s[i] == 'r':
		dp[2] = min(dp[1], a[i] + dp[2])
	elif s[i] == 'd':
		dp[3] = min(dp[2], a[i] + dp[3])
print(min(dp))


import copy
n = int(input())
a = input()
w = list(map(int, input().split()))
dp = [[float('inf')] * 4 for _ in range(n)]
dp[-1] = [0, 0, 0, 0]
if a[-1] == 'd':
	dp[-1][3] = w[-1]
l = [n] * 4
d = {'h': 0, 'a': 1, 'r': 2, 'd': 3}
for i in range(n - 2, -1, -1):
	ind = d.get(a[i], -1)
	delta = 0
	for j in range(4):
		dp[i] = dp[i + 1]
		if j == ind:
			if l[j] < n:
				delta = copy.deepcopy(dp[l[j]][j])
			if j <= 2:
				dp[i][j] = copy.deepcopy(min(w[i] + delta, dp[i + 1][j + 1]))
			else:
				dp[i][j] = w[i] + dp[i + 1][j]
			l[j] = i
print(dp[0][0])


n = int(input())
word = list(input())
costs = list(map(int, input().rstrip().split()))
costlist = [0] * 4
for i in range(n):
	if word[i] == 'h':
		costlist[0] += costs[i]
	if word[i] == 'a':
		costlist[1] = min(costlist[0], costlist[1] + costs[i])
	if word[i] == 'r':
		costlist[2] = min(costlist[1], costlist[2] + costs[i])
	if word[i] == 'd':
		costlist[3] = min(costlist[2], costlist[3] + costs[i])
print(costlist[3])


input()
res_h = 0
res_ha = 0
res_har = 0
res_hard = 0
for (s, a) in zip(input(), list(map(int, input().split()))):
	if s == 'h':
		res_h += a
	elif s == 'a':
		res_ha = min(res_ha + a, res_h)
	elif s == 'r':
		res_har = min(res_har + a, res_ha)
	elif s == 'd':
		res_hard = min(res_hard + a, res_har)
print(res_hard)


import math
n = int(input())
s = input()
c = list(map(int, input().split()))
h = [0] * n
ta = 0
tr = 0
td = 0
for i in range(n):
	if s[i] == 'h':
		h[i] = c[i]
	if i:
		h[i] = h[i] + h[i - 1]
	if s[i] == 'a':
		ta = min(ta + c[i], h[i])
	elif s[i] == 'r':
		tr = min(tr + c[i], ta)
	elif s[i] == 'd':
		td = min(td + c[i], tr)
print(td)


input()
dp = [0] * 5
for (i, c) in zip(map('hard'.find, input()), map(int, input().split())):
	if i < 0:
		continue
	dp[i + 1] = min(dp[i + 1], dp[i])
	dp[i] += c
print(min(dp[:-1]))


import sys
n = int(sys.stdin.readline().strip())
s = sys.stdin.readline().strip()
a = list(map(int, sys.stdin.readline().strip().split()))
amb = 0
h = [0] * (n + 1)
ha = [0] * (n + 1)
har = [0] * (n + 1)
hard = [0] * (n + 1)
for i in range(1, n + 1):
	if s[i - 1] == 'h':
		h[i] = h[i - 1] + a[i - 1]
	else:
		h[i] = h[i - 1]
	if s[i - 1] == 'a':
		ha[i] = min([ha[i - 1] + a[i - 1], h[i - 1]])
	else:
		ha[i] = ha[i - 1]
	if s[i - 1] == 'r':
		har[i] = min([har[i - 1] + a[i - 1], ha[i - 1]])
	else:
		har[i] = har[i - 1]
	if s[i - 1] == 'd':
		hard[i] = min([hard[i - 1] + a[i - 1], har[i - 1]])
	else:
		hard[i] = hard[i - 1]
print(hard[n])


from math import inf

def main():
	count = int(input())
	string = input()
	arr = input().split()
	hard = 'hard'
	dp = [[inf for y in range(5)] for x in range(count + 1)]
	dp[0][0] = 0
	for x in range(count):
		for y in range(4):
			if string[x] == hard[y]:
				dp[x + 1][y + 1] = min(dp[x][y], dp[x + 1][y + 1])
				dp[x + 1][y] = min(dp[x][y] + int(arr[x]), dp[x + 1][y])
			else:
				dp[x + 1][y] = min(dp[x][y], dp[x + 1][y])
	mini = inf
	for x in range(4):
		mini = min(mini, dp[-1][x])
	print(mini)
main()


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	s = list(input())
	arr = list(map(int, input().split()))
	(h, ha, har, hard) = (0, 0, 0, 0)
	for (i, item) in enumerate(s):
		if item == 'h':
			h += arr[i]
		elif item == 'a':
			ha = min(h, ha + arr[i])
		elif item == 'r':
			har = min(ha, har + arr[i])
		elif item == 'd':
			hard = min(har, hard + arr[i])
	print(hard)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def divisors(M):
	d = []
	i = 1
	while M >= i ** 2:
		if M % i == 0:
			d.append(i)
			if i ** 2 != M:
				d.append(M // i)
		i = i + 1
	return d

def popcount(x):
	x = x - (x >> 1 & 1431655765)
	x = (x & 858993459) + (x >> 2 & 858993459)
	x = x + (x >> 4) & 252645135
	x = x + (x >> 8)
	x = x + (x >> 16)
	return x & 127

def eratosthenes(n):
	res = [0 for i in range(n + 1)]
	prime = set([])
	for i in range(2, n + 1):
		if not res[i]:
			prime.add(i)
			for j in range(1, n // i + 1):
				res[i * j] = 1
	return prime

def factorization(n):
	res = []
	for p in prime:
		if n % p == 0:
			while n % p == 0:
				n //= p
			res.append(p)
	if n != 1:
		res.append(n)
	return res

def euler_phi(n):
	res = n
	for x in range(2, n + 1):
		if x ** 2 > n:
			break
		if n % x == 0:
			res = res // x * (x - 1)
			while n % x == 0:
				n //= x
	if n != 1:
		res = res // n * (n - 1)
	return res

def ind(b, n):
	res = 0
	while n % b == 0:
		res += 1
		n //= b
	return res

def isPrimeMR(n):
	if n == 1:
		return 0
	d = n - 1
	d = d // (d & -d)
	L = [2, 3, 5, 7, 11, 13, 17]
	for a in L:
		t = d
		y = pow(a, t, n)
		if y == 1:
			continue
		while y != n - 1:
			y = y * y % n
			if y == 1 or t == n - 1:
				return 0
			t <<= 1
	return 1

def findFactorRho(n):
	from math import gcd
	m = 1 << n.bit_length() // 8
	for c in range(1, 99):
		f = lambda x: (x * x + c) % n
		(y, r, q, g) = (2, 1, 1, 1)
		while g == 1:
			x = y
			for i in range(r):
				y = f(y)
			k = 0
			while k < r and g == 1:
				ys = y
				for i in range(min(m, r - k)):
					y = f(y)
					q = q * abs(x - y) % n
				g = gcd(q, n)
				k += m
			r <<= 1
		if g == n:
			g = 1
			while g == 1:
				ys = f(ys)
				g = gcd(abs(x - ys), n)
		if g < n:
			if isPrimeMR(g):
				return g
			elif isPrimeMR(n // g):
				return n // g
			return findFactorRho(g)

def primeFactor(n):
	i = 2
	ret = {}
	rhoFlg = 0
	while i * i <= n:
		k = 0
		while n % i == 0:
			n //= i
			k += 1
		if k:
			ret[i] = k
		i += 1 + i % 2
		if i == 101 and n >= 2 ** 20:
			while n > 1:
				if isPrimeMR(n):
					(ret[n], n) = (1, 1)
				else:
					rhoFlg = 1
					j = findFactorRho(n)
					k = 0
					while n % j == 0:
						n //= j
						k += 1
					ret[j] = k
	if n > 1:
		ret[n] = 1
	if rhoFlg:
		ret = {x: ret[x] for x in sorted(ret)}
	return ret

def divisors(n):
	res = [1]
	prime = primeFactor(n)
	for p in prime:
		newres = []
		for d in res:
			for j in range(prime[p] + 1):
				newres.append(d * p ** j)
		res = newres
	res.sort()
	return res

def xorfactorial(num):
	if num == 0:
		return 0
	elif num == 1:
		return 1
	elif num == 2:
		return 3
	elif num == 3:
		return 0
	else:
		x = baseorder(num)
		return 2 ** x * ((num - 2 ** x + 1) % 2) + function(num - 2 ** x)

def xorconv(n, X, Y):
	if n == 0:
		res = [X[0] * Y[0] % mod]
		return res
	x = [X[i] + X[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]
	y = [Y[i] + Y[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]
	z = [X[i] - X[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]
	w = [Y[i] - Y[i + 2 ** (n - 1)] for i in range(2 ** (n - 1))]
	res1 = xorconv(n - 1, x, y)
	res2 = xorconv(n - 1, z, w)
	former = [(res1[i] + res2[i]) * inv for i in range(2 ** (n - 1))]
	latter = [(res1[i] - res2[i]) * inv for i in range(2 ** (n - 1))]
	former = list(map(lambda x: x % mod, former))
	latter = list(map(lambda x: x % mod, latter))
	return former + latter

def merge_sort(A, B):
	(pos_A, pos_B) = (0, 0)
	(n, m) = (len(A), len(B))
	res = []
	while pos_A < n and pos_B < m:
		(a, b) = (A[pos_A], B[pos_B])
		if a < b:
			res.append(a)
			pos_A += 1
		else:
			res.append(b)
			pos_B += 1
	res += A[pos_A:]
	res += B[pos_B:]
	return res

class UnionFindVerSize:

	def __init__(self, N):
		self._parent = [n for n in range(0, N)]
		self._size = [1] * N
		self.group = N

	def find_root(self, x):
		if self._parent[x] == x:
			return x
		self._parent[x] = self.find_root(self._parent[x])
		stack = [x]
		while self._parent[stack[-1]] != stack[-1]:
			stack.append(self._parent[stack[-1]])
		for v in stack:
			self._parent[v] = stack[-1]
		return self._parent[x]

	def unite(self, x, y):
		gx = self.find_root(x)
		gy = self.find_root(y)
		if gx == gy:
			return
		self.group -= 1
		if self._size[gx] < self._size[gy]:
			self._parent[gx] = gy
			self._size[gy] += self._size[gx]
		else:
			self._parent[gy] = gx
			self._size[gx] += self._size[gy]

	def get_size(self, x):
		return self._size[self.find_root(x)]

	def is_same_group(self, x, y):
		return self.find_root(x) == self.find_root(y)

class WeightedUnionFind:

	def __init__(self, N):
		self.parent = [i for i in range(N)]
		self.size = [1 for i in range(N)]
		self.val = [0 for i in range(N)]
		self.flag = True
		self.edge = [[] for i in range(N)]

	def dfs(self, v, pv):
		stack = [(v, pv)]
		new_parent = self.parent[pv]
		while stack:
			(v, pv) = stack.pop()
			self.parent[v] = new_parent
			for (nv, w) in self.edge[v]:
				if nv != pv:
					self.val[nv] = self.val[v] + w
					stack.append((nv, v))

	def unite(self, x, y, w):
		if not self.flag:
			return
		if self.parent[x] == self.parent[y]:
			self.flag = self.val[x] - self.val[y] == w
			return
		if self.size[self.parent[x]] > self.size[self.parent[y]]:
			self.edge[x].append((y, -w))
			self.edge[y].append((x, w))
			self.size[x] += self.size[y]
			self.val[y] = self.val[x] - w
			self.dfs(y, x)
		else:
			self.edge[x].append((y, -w))
			self.edge[y].append((x, w))
			self.size[y] += self.size[x]
			self.val[x] = self.val[y] + w
			self.dfs(x, y)

class Dijkstra:

	class Edge:

		def __init__(self, _to, _cost):
			self.to = _to
			self.cost = _cost

	def __init__(self, V):
		self.G = [[] for i in range(V)]
		self._E = 0
		self._V = V

	@property
	def E(self):
		return self._E

	@property
	def V(self):
		return self._V

	def add_edge(self, _from, _to, _cost):
		self.G[_from].append(self.Edge(_to, _cost))
		self._E += 1

	def shortest_path(self, s):
		import heapq
		que = []
		d = [10 ** 15] * self.V
		d[s] = 0
		heapq.heappush(que, (0, s))
		while len(que) != 0:
			(cost, v) = heapq.heappop(que)
			if d[v] < cost:
				continue
			for i in range(len(self.G[v])):
				e = self.G[v][i]
				if d[e.to] > d[v] + e.cost:
					d[e.to] = d[v] + e.cost
					heapq.heappush(que, (d[e.to], e.to))
		return d

def Z_algorithm(s):
	N = len(s)
	Z_alg = [0] * N
	Z_alg[0] = N
	i = 1
	j = 0
	while i < N:
		while i + j < N and s[j] == s[i + j]:
			j += 1
		Z_alg[i] = j
		if j == 0:
			i += 1
			continue
		k = 1
		while i + k < N and k + Z_alg[k] < j:
			Z_alg[i + k] = Z_alg[k]
			k += 1
		i += k
		j -= k
	return Z_alg

class BIT:

	def __init__(self, n, mod=None):
		self.BIT = [0] * (n + 1)
		self.num = n
		self.mod = mod

	def query(self, idx):
		res_sum = 0
		mod = self.mod
		while idx > 0:
			res_sum += self.BIT[idx]
			if mod:
				res_sum %= mod
			idx -= idx & -idx
		return res_sum

	def update(self, idx, x):
		mod = self.mod
		while idx <= self.num:
			self.BIT[idx] += x
			self.BIT[idx] %= mod
			idx += idx & -idx
		return

class dancinglink:

	def __init__(self, n, debug=False):
		self.n = n
		self.debug = debug
		self._left = [i - 1 for i in range(n)]
		self._right = [i + 1 for i in range(n)]
		self.exist = [True for i in range(n)]

	def pop(self, k):
		if self.debug:
			assert self.exist[k]
		L = self._left[k]
		R = self._right[k]
		if L != -1:
			if R != self.n:
				(self._right[L], self._left[R]) = (R, L)
			else:
				self._right[L] = self.n
		elif R != self.n:
			self._left[R] = -1
		self.exist[k] = False

	def left(self, idx, k=1):
		if self.debug:
			assert self.exist[idx]
		res = idx
		while k:
			res = self._left[res]
			if res == -1:
				break
			k -= 1
		return res

	def right(self, idx, k=1):
		if self.debug:
			assert self.exist[idx]
		res = idx
		while k:
			res = self._right[res]
			if res == self.n:
				break
			k -= 1
		return res

class SparseTable:

	def __init__(self, A, merge_func, ide_ele):
		N = len(A)
		n = N.bit_length()
		self.table = [[ide_ele for i in range(n)] for i in range(N)]
		self.merge_func = merge_func
		for i in range(N):
			self.table[i][0] = A[i]
		for j in range(1, n):
			for i in range(0, N - 2 ** j + 1):
				f = self.table[i][j - 1]
				s = self.table[i + 2 ** (j - 1)][j - 1]
				self.table[i][j] = self.merge_func(f, s)

	def query(self, s, t):
		b = t - s + 1
		m = b.bit_length() - 1
		return self.merge_func(self.table[s][m], self.table[t - 2 ** m + 1][m])

class BinaryTrie:

	class node:

		def __init__(self, val):
			self.left = None
			self.right = None
			self.max = val

	def __init__(self):
		self.root = self.node(-10 ** 15)

	def append(self, key, val):
		pos = self.root
		for i in range(29, -1, -1):
			pos.max = max(pos.max, val)
			if key >> i & 1:
				if pos.right is None:
					pos.right = self.node(val)
					pos = pos.right
				else:
					pos = pos.right
			elif pos.left is None:
				pos.left = self.node(val)
				pos = pos.left
			else:
				pos = pos.left
		pos.max = max(pos.max, val)

	def search(self, M, xor):
		res = -10 ** 15
		pos = self.root
		for i in range(29, -1, -1):
			if pos is None:
				break
			if M >> i & 1:
				if xor >> i & 1:
					if pos.right:
						res = max(res, pos.right.max)
					pos = pos.left
				else:
					if pos.left:
						res = max(res, pos.left.max)
					pos = pos.right
			elif xor >> i & 1:
				pos = pos.right
			else:
				pos = pos.left
		if pos:
			res = max(res, pos.max)
		return res

def solveequation(edge, ans, n, m):
	x = [0] * m
	used = [False] * n
	for v in range(n):
		if used[v]:
			continue
		y = dfs(v)
		if y != 0:
			return False
	return x

	def dfs(v):
		used[v] = True
		r = ans[v]
		for (to, dire, id) in edge[v]:
			if used[to]:
				continue
			y = dfs(to)
			if dire == -1:
				x[id] = y
			else:
				x[id] = -y
			r += y
		return r

class Matrix:
	mod = 10 ** 9 + 7

	def set_mod(m):
		Matrix.mod = m

	def __init__(self, L):
		self.row = len(L)
		self.column = len(L[0])
		self._matrix = L
		for i in range(self.row):
			for j in range(self.column):
				self._matrix[i][j] %= Matrix.mod

	def __getitem__(self, item):
		if type(item) == int:
			raise IndexError('you must specific row and column')
		elif len(item) != 2:
			raise IndexError('you must specific row and column')
		(i, j) = item
		return self._matrix[i][j]

	def __setitem__(self, item, val):
		if type(item) == int:
			raise IndexError('you must specific row and column')
		elif len(item) != 2:
			raise IndexError('you must specific row and column')
		(i, j) = item
		self._matrix[i][j] = val

	def __add__(self, other):
		if (self.row, self.column) != (other.row, other.column):
			raise SizeError('sizes of matrixes are different')
		res = [[0 for j in range(self.column)] for i in range(self.row)]
		for i in range(self.row):
			for j in range(self.column):
				res[i][j] = self._matrix[i][j] + other._matrix[i][j]
				res[i][j] %= Matrix.mod
		return Matrix(res)

	def __sub__(self, other):
		if (self.row, self.column) != (other.row, other.column):
			raise SizeError('sizes of matrixes are different')
		res = [[0 for j in range(self.column)] for i in range(self.row)]
		for i in range(self.row):
			for j in range(self.column):
				res[i][j] = self._matrix[i][j] - other._matrix[i][j]
				res[i][j] %= Matrix.mod
		return Matrix(res)

	def __mul__(self, other):
		if type(other) != int:
			if self.column != other.row:
				raise SizeError('sizes of matrixes are different')
			res = [[0 for j in range(other.column)] for i in range(self.row)]
			for i in range(self.row):
				for j in range(other.column):
					temp = 0
					for k in range(self.column):
						temp += self._matrix[i][k] * other._matrix[k][j]
					res[i][j] = temp % Matrix.mod
			return Matrix(res)
		else:
			n = other
			res = [[n * self._matrix[i][j] % Matrix.mod for j in range(self.column)] for i in range(self.row)]
			return Matrix(res)

	def __pow__(self, m):
		if self.column != self.row:
			raise MatrixPowError('the size of row must be the same as that of column')
		n = self.row
		res = Matrix([[int(i == j) for i in range(n)] for j in range(n)])
		while m:
			if m % 2 == 1:
				res = res * self
			self = self * self
			m //= 2
		return res

	def __str__(self):
		res = []
		for i in range(self.row):
			for j in range(self.column):
				res.append(str(self._matrix[i][j]))
				res.append(' ')
			res.append('\n')
		res = res[:len(res) - 1]
		return ''.join(res)

class SegmentTree:

	def __init__(self, init_val, segfunc, ide_ele):
		n = len(init_val)
		self.segfunc = segfunc
		self.ide_ele = ide_ele
		self.num = 1 << (n - 1).bit_length()
		self.tree = [ide_ele] * 2 * self.num
		for i in range(n):
			self.tree[self.num + i] = init_val[i]
		for i in range(self.num - 1, 0, -1):
			self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

	def update(self, k, x):
		k += self.num
		self.tree[k] = x
		while k > 1:
			self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
			k >>= 1

	def query(self, l, r):
		res = self.ide_ele
		l += self.num
		r += self.num
		while l < r:
			if l & 1:
				res = self.segfunc(res, self.tree[l])
				l += 1
			if r & 1:
				res = self.segfunc(res, self.tree[r - 1])
			l >>= 1
			r >>= 1
		return res

	def bisect_l(self, l, r, x):
		l += self.num
		r += self.num
		Lmin = -1
		Rmin = -1
		while l < r:
			if l & 1:
				if self.tree[l] <= x and Lmin == -1:
					Lmin = l
				l += 1
			if r & 1:
				if self.tree[r - 1] <= x:
					Rmin = r - 1
			l >>= 1
			r >>= 1
		if Lmin != -1:
			pos = Lmin
			while pos < self.num:
				if self.tree[2 * pos] <= x:
					pos = 2 * pos
				else:
					pos = 2 * pos + 1
			return pos - self.num
		elif Rmin != -1:
			pos = Rmin
			while pos < self.num:
				if self.tree[2 * pos] <= x:
					pos = 2 * pos
				else:
					pos = 2 * pos + 1
			return pos - self.num
		else:
			return -1
import sys, random, bisect
from collections import deque, defaultdict
from heapq import heapify, heappop, heappush
from itertools import permutations
from math import log, gcd
input = lambda : sys.stdin.readline().rstrip()
mi = lambda : map(int, input().split())
li = lambda : list(mi())
n = int(input())
s = '?' + input()
a = li()
dp = [[10 ** 17 for i in range(4)] for j in range(n + 1)]
dp[0][0] = 0
for i in range(1, n + 1):
	if s[i] == 'h':
		dp[i][0] = dp[i - 1][0] + a[i - 1]
		dp[i][1] = min(dp[i - 1][1], dp[i - 1][0])
		dp[i][2] = dp[i - 1][2]
		dp[i][3] = dp[i - 1][3]
	elif s[i] == 'a':
		dp[i][0] = dp[i - 1][0]
		dp[i][1] = dp[i - 1][1] + a[i - 1]
		dp[i][2] = min(dp[i - 1][2], dp[i - 1][1])
		dp[i][3] = dp[i - 1][3]
	elif s[i] == 'r':
		dp[i][0] = dp[i - 1][0]
		dp[i][1] = dp[i - 1][1]
		dp[i][2] = dp[i - 1][2] + a[i - 1]
		dp[i][3] = min(dp[i - 1][3], dp[i - 1][2])
	elif s[i] == 'd':
		dp[i][0] = dp[i - 1][0]
		dp[i][1] = dp[i - 1][1]
		dp[i][2] = dp[i - 1][2]
		dp[i][3] = dp[i - 1][3] + a[i - 1]
	else:
		dp[i][0] = dp[i - 1][0]
		dp[i][1] = dp[i - 1][1]
		dp[i][2] = dp[i - 1][2]
		dp[i][3] = dp[i - 1][3]
print(min(dp[n]))


n = int(input())
s = input()
arr = list(map(int, input().split()))
dp = {}
for i in range(n):
	dp[i] = {}
	for j in range(4):
		if i == 0 and j == 0:
			if s[i] == 'h':
				dp[i][j] = arr[i]
			else:
				dp[i][j] = 0
		elif i == 0:
			dp[i][j] = 0
		elif j == 0:
			dp[i][j] = dp[i - 1][j]
			if s[i] == 'h':
				dp[i][j] += arr[i]
		elif j == 1:
			if s[i] != 'a':
				dp[i][j] = dp[i - 1][j]
			else:
				dp[i][j] = min(arr[i] + dp[i - 1][j], dp[i - 1][j - 1])
		elif j == 2:
			if s[i] != 'r':
				dp[i][j] = dp[i - 1][j]
			else:
				dp[i][j] = min(arr[i] + dp[i - 1][j], dp[i - 1][j - 1])
		elif s[i] != 'd':
			dp[i][j] = dp[i - 1][j]
		else:
			dp[i][j] = min(arr[i] + dp[i - 1][j], dp[i - 1][j - 1])
print(dp[n - 1][3])


n = int(input())
s = input()
dp = [0] * 4
a = list(map(int, input().split()))
for i in range(len(s)):
	if s[i] == 'h':
		dp[0] += a[i]
	elif s[i] == 'a':
		dp[1] = min(dp[0], dp[1] + a[i])
	elif s[i] == 'r':
		dp[2] = min(dp[1], dp[2] + a[i])
	elif s[i] == 'd':
		dp[3] = min(dp[2], dp[3] + a[i])
print(min(dp))


import os, sys
from io import BytesIO, IOBase
from math import inf

def main():
	n = int(input())
	s = input().strip()
	a = list(map(int, input().split()))
	dp = [[inf] * 4 for _ in range(n + 1)]
	dp[0][0] = 0
	for ind in range(1, n + 1):
		dp[ind] = dp[ind - 1][:]
		if s[ind - 1] == 'h':
			dp[ind][1] = min(dp[ind - 1][1], dp[ind - 1][0])
			dp[ind][0] += a[ind - 1]
		elif s[ind - 1] == 'a':
			dp[ind][2] = min(dp[ind - 1][2], dp[ind - 1][1])
			dp[ind][1] += a[ind - 1]
		elif s[ind - 1] == 'r':
			dp[ind][3] = min(dp[ind - 1][3], dp[ind - 1][2])
			dp[ind][2] += a[ind - 1]
		elif s[ind - 1] == 'd':
			dp[ind][3] += a[ind - 1]
	print(min(dp[-1]))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


l = int(input())
s = input()
li = list(map(int, input().split()))
h_val = 0
a_val = 0
r_val = 0
d_val = 0
if s[0] == 'h':
	h_val = li[0]
for i in range(1, l):
	if s[i] == 'h':
		h_val += li[i]
	elif s[i] == 'a':
		a_val = min(a_val + li[i], h_val)
	elif s[i] == 'r':
		r_val = min(r_val + li[i], a_val)
	elif s[i] == 'd':
		d_val = min(d_val + li[i], r_val)
print(min(min(a_val, r_val), min(d_val, h_val)))


n = int(input())
s = input()
t = 'hard'
a = list(map(int, input().split()))
dp = [x[:] for x in [[0] * 4] * (n + 1)]
for i in range(1, n + 1):
	for j in range(4):
		if s[i - 1] == t[j]:
			if j == 0:
				dp[i][j] = dp[i - 1][j] + a[i - 1]
			else:
				dp[i][j] = min(dp[i - 1][j] + a[i - 1], dp[i - 1][j - 1])
		else:
			dp[i][j] = dp[i - 1][j]
res = float('inf')
for i in range(4):
	res = min(res, dp[n][i])
print(res)


inf = int(1000000000000000.0)
n = int(input())
s = input()
a = list(map(int, input().split()))

def vec(a, b, v):
	return [[v for c in range(b)] for r in range(a)]
dp = vec(n + 1, 4, inf)
dp[0][0] = 0
for i in range(1, n + 1):
	if s[i - 1] not in 'hard':
		for j in range(4):
			dp[i][j] = dp[i - 1][j]
	elif s[i - 1] == 'h':
		dp[i][0] = dp[i - 1][0] + a[i - 1]
		dp[i][1] = min(dp[i - 1][1], dp[i - 1][0])
		dp[i][2] = dp[i - 1][2]
		dp[i][3] = dp[i - 1][3]
	elif s[i - 1] == 'a':
		dp[i][0] = dp[i - 1][0]
		dp[i][1] = dp[i - 1][1] + a[i - 1]
		dp[i][2] = min(dp[i - 1][2], dp[i - 1][1])
		dp[i][3] = dp[i - 1][3]
	elif s[i - 1] == 'r':
		dp[i][0] = dp[i - 1][0]
		dp[i][1] = dp[i - 1][1]
		dp[i][2] = dp[i - 1][2] + a[i - 1]
		dp[i][3] = min(dp[i - 1][3], dp[i - 1][2])
	elif s[i - 1] == 'd':
		dp[i][0] = dp[i - 1][0]
		dp[i][1] = dp[i - 1][1]
		dp[i][2] = dp[i - 1][2]
		dp[i][3] = dp[i - 1][3] + a[i - 1]
print(min(dp[n][0], dp[n][1], dp[n][2], dp[n][3]))


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify, nlargest
from copy import deepcopy
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = sys.stdin.readline()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
n = inp()
s = inpsl(n)
a = inpl()
dp = [[INF] * (n + 1) for _ in range(5)]
dp[0][0] = 0
d = 'hard'
for (i, x) in enumerate(s):
	for j in range(4):
		tmp = a[i] if x == d[j] else 0
		dp[j][i + 1] = min(dp[j][i + 1], dp[j][i] + tmp)
		dp[j + 1][i + 1] = min(dp[j + 1][i + 1], dp[j][i])
res = INF
for j in range(4):
	res = min(res, dp[j][-1])
print(res)


a = [0 for i in range(100010)]
dp = [[0 for i in range(4)] for j in range(100010)]
ans = 1e+18
t = 'hard'
n = int(input())
s = input()
s = ' ' + s
for i in range(n + 1):
	for j in range(4):
		dp[i][j] = 1e+18
dp[0][0] = 0
a = [0] + list(map(int, input().split()))
for x in a:
	dp[0][0] += x
for i in range(1, n + 1):
	p = t.find(s[i])
	for j in range(4):
		if j == p:
			if j < 3:
				dp[i][j + 1] = min(dp[i][j + 1], dp[i - 1][j] - a[i])
			dp[i][j] = min(dp[i][j], dp[i - 1][j])
		else:
			dp[i][j] = min(dp[i][j], dp[i - 1][j] - a[i])
for i in range(4):
	ans = min(ans, dp[n][i])
print(ans)


n = int(input())
s = input()
aa = input().split()
a = []
for x in aa:
	a.append(int(x))
f = [0, 1e+18, 1e+18, 1e+18]
for i in range(0, n):
	if s[i] == 'h':
		f[1] = min(f[1], f[0])
		f[0] += a[i]
	elif s[i] == 'a':
		f[2] = min(f[2], f[1])
		f[1] += a[i]
	elif s[i] == 'r':
		f[3] = min(f[3], f[2])
		f[2] += a[i]
	elif s[i] == 'd':
		f[3] += a[i]
print(min(f))


import sys

def solve():
	n = int(input())
	s = input()
	cost_str = input().split()
	cost = [int(x) for x in cost_str]
	dp = [[10 ** 18 + 7] * 5 for _ in range(n + 1)]
	t = 'hard'
	for i in range(1, 5):
		dp[0][i] = 0
	for i in range(1, n + 1):
		for j in range(1, 5):
			if s[i - 1] != t[j - 1]:
				dp[i][j] = dp[i - 1][j]
			else:
				dp[i][j] = min(dp[i - 1][j] + cost[i - 1], dp[i - 1][j - 1])
	print(dp[n][4])
	return 0
solve()


n = int(input())
s = list(input())
c = list(map(int, input().split()))
h = a = r = d = 0
for i in range(n):
	if s[i] == 'h':
		h += c[i]
	elif s[i] == 'a':
		a = min(h, a + c[i])
	elif s[i] == 'r':
		r = min(a, r + c[i])
	elif s[i] == 'd':
		d = min(r, d + c[i])
print(d)


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def list2d(a, b, c):
	return [[c] * b for i in range(a)]
MOD = 1000000000 + 7

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
N = int(input())
S = input()
A = alele()
(h, a, r, d) = (0, 0, 0, 0)
for i in range(N):
	if S[i] == 'h':
		h += A[i]
	elif S[i] == 'a':
		a = min(h, a + A[i])
	elif S[i] == 'r':
		r = min(a, r + A[i])
	elif S[i] == 'd':
		d = min(r, d + A[i])
print(d)


num = int(input().strip())
string = input().strip()
cost = [int(i) for i in input().strip().split()]
hard_list = ['.', 'h', 'a', 'r', 'd']
ans = [[10000000000000000000000000000, 0, 0, 0, 0] for i in range(num + 1)]
for i in range(1, num + 1):
	for j in range(1, 5):
		if string[i - 1] == hard_list[j]:
			ans[i][j] = min(ans[i - 1][j - 1], ans[i - 1][j] + cost[i - 1])
		else:
			ans[i][j] = ans[i - 1][j]
print(ans[-1][-1])


from itertools import combinations
from sys import stdin, stdout

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def check(s1, s2):
	s2 = list(s2)
	try:
		(i1, i2, i3, i4) = (s2.index('h'), s2.index('a'), s2.index('r'), s2.index('d'))
		if i1 < i2 < i3 < i4:
			return 1
		else:
			return 0
	except:
		return 0

def solve(n, s, a):
	dp = [0] * 4
	string = 'hard'
	minn = float('inf')
	for i in range(n):
		if s[i] in string:
			if s[i] == 'h':
				dp[0] += a[i]
			else:
				for j in range(1, 4):
					if s[i] == string[j]:
						dp[j] = min(dp[j - 1], dp[j] + a[i])
	return dp[3]
n = intIn()
s = stdin.readline()
a = listIn()
res = solve(n, s, a)
print(res)


import math
n = int(input())
s = input()
A = [int(x) for x in input().split(' ')]

def f():
	dp = [[0] * 5 for _ in [-1] * n]
	for i in reversed(range(n)):
		for hard in reversed(range(4)):
			if i + 1 == n:
				temp1 = A[i]
				if s[i] == 'hard'[hard]:
					if hard + 1 == 4:
						temp2 = math.inf
					else:
						temp2 = 0
				else:
					temp2 = 0
			else:
				temp1 = A[i] + dp[i + 1][hard]
				if s[i] == 'hard'[hard]:
					if hard + 1 == 4:
						temp2 = math.inf
					else:
						temp2 = dp[i + 1][hard + 1]
				else:
					temp2 = dp[i + 1][hard]
			dp[i][hard] = min(temp1, temp2)
	return dp[0][0]
print(f())


t = int(input())
nums = input()
arr = list(map(int, input().split()))
h = a = r = d = 0
for i in range(t):
	if nums[i] == 'h':
		h += arr[i]
	elif nums[i] == 'a':
		a = min(h, a + arr[i])
	elif nums[i] == 'r':
		r = min(a, r + arr[i])
	elif nums[i] == 'd':
		d = min(r, d + arr[i])
print(d)


n = int(input())
s = list(input())
a = list(map(int, input().split()))
INF = 1 << 60
dp = [[0] * 5 for i in range(n + 1)]
HARD_STRING = list('hard')
for (idx, char) in enumerate(s):
	if char not in HARD_STRING:
		for j in range(5):
			dp[idx + 1][j] = dp[idx][j]
		continue
	else:
		cost = a[idx]
		if char == 'h':
			dp[idx + 1][0] = dp[idx][0] + cost
			dp[idx + 1][1] = min(dp[idx][1], dp[idx][0])
			dp[idx + 1][2] = dp[idx][2]
			dp[idx + 1][3] = dp[idx][3]
			dp[idx + 1][4] = dp[idx][4]
			continue
		elif char == 'a':
			dp[idx + 1][0] = dp[idx][0]
			dp[idx + 1][1] = dp[idx][1] + cost
			dp[idx + 1][2] = min(dp[idx][2], dp[idx][1])
			dp[idx + 1][3] = dp[idx][3]
			dp[idx + 1][4] = dp[idx][4]
			continue
		elif char == 'r':
			dp[idx + 1][0] = dp[idx][0]
			dp[idx + 1][1] = dp[idx][1]
			dp[idx + 1][2] = dp[idx][2] + cost
			dp[idx + 1][3] = min(dp[idx][3], dp[idx][2])
			dp[idx + 1][4] = dp[idx][4]
			continue
		elif char == 'd':
			dp[idx + 1][0] = dp[idx][0]
			dp[idx + 1][1] = dp[idx][1]
			dp[idx + 1][2] = dp[idx][2]
			dp[idx + 1][3] = dp[idx][3] + cost
			dp[idx + 1][4] = min(dp[idx][4], dp[idx][3])
			continue
ans = min((dp[n][c] for c in range(4)))
print(ans)


from sys import stdin
n = int(stdin.readline().strip())
string = list(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
dp = [[0 for i in range(n + 1)] for j in range(5)]
for i in range(n + 1):
	dp[0][i] = float('inf')
t = 'hard'
for i in range(n):
	for j in range(4):
		if string[i] == t[j]:
			dp[j + 1][i + 1] = min(dp[j][i], dp[j + 1][i] + arr[i])
		else:
			dp[j + 1][i + 1] = dp[j + 1][i]
print(dp[4][n])


lens = int(input())
(res_h, res_a, res_r, res_d) = (0, 0, 0, 0)
for (ch, amb) in zip(input(), [int(x) for x in input().split()]):
	if ch == 'h':
		res_h += amb
	elif ch == 'a':
		res_a = min(res_a + amb, res_h)
	elif ch == 'r':
		res_r = min(res_r + amb, res_a)
	elif ch == 'd':
		res_d = min(res_d + amb, res_r)
print(res_d)


n = int(input())
s = input()
arr = list(map(int, input().split()))
t = 'hard'
dp = {}
for i in range(n):
	dp[i] = {}
	for j in range(4):
		if i == 0 and j == 0:
			if s[i] == 'h':
				dp[i][j] = arr[i]
			else:
				dp[i][j] = 0
		elif i == 0:
			dp[i][j] = 0
		elif j == 0:
			dp[i][j] = dp[i - 1][j]
			if s[i] == 'h':
				dp[i][j] += arr[i]
		elif s[i] != t[j]:
			dp[i][j] = dp[i - 1][j]
		else:
			dp[i][j] = min(arr[i] + dp[i - 1][j], dp[i - 1][j - 1])
print(dp[n - 1][3])


import collections
import time
import os
import sys
import bisect
import heapq
N = int(input())
S = input()
A = [int(x) for x in input().split()]
(h, a, r, d) = (0, 0, 0, 0)
for (i, v) in enumerate(S):
	if v == 'h':
		h += A[i]
	elif v == 'a':
		a = min(h, a + A[i])
	elif v == 'r':
		r = min(a, r + A[i])
	elif v == 'd':
		d = min(r, d + A[i])
print(min(h, a, r, d))


import sys
n = int(input())
word = input()
costs = [int(x) for x in input().split()]
dp = [[10000000000000000 for _ in range(5)] for _ in range(n + 1)]
for i in range(5):
	dp[0][i] = 0
for i in range(1, n + 1):
	for k in range(5):
		if k == 0:
			if word[i - 1] == 'h':
				dp[i][0] = dp[i - 1][0] + costs[i - 1]
			else:
				dp[i][0] = dp[i - 1][0]
		if k == 1:
			if word[i - 1] == 'a':
				dp[i][1] = min(dp[i - 1][1] + costs[i - 1], dp[i - 1][0])
			elif word[i - 1] == 'h':
				dp[i][1] = min(dp[i - 1][0], dp[i - 1][1])
			else:
				dp[i][1] = dp[i - 1][1]
		if k == 2:
			if word[i - 1] == 'r':
				dp[i][2] = min(dp[i - 1][2] + costs[i - 1], dp[i - 1][1])
			elif word[i - 1] == 'a':
				dp[i][2] = min(dp[i - 1][2], dp[i - 1][1])
			else:
				dp[i][2] = dp[i - 1][2]
		if k == 3:
			if word[i - 1] == 'd':
				dp[i][3] = min(dp[i - 1][3] + costs[i - 1], dp[i - 1][2])
			elif word[i - 1] == 'r':
				dp[i][3] = min(dp[i - 1][3], dp[i - 1][2])
			else:
				dp[i][3] = dp[i - 1][3]
print(min(dp[n]))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')

def main():
	n = N()
	s = input()
	arr = RLL()
	h = a = r = d = 0
	for i in range(n):
		if s[i] == 'h':
			h += arr[i]
		elif s[i] == 'a':
			a = min(h, a + arr[i])
		elif s[i] == 'r':
			r = min(a, r + arr[i])
		elif s[i] == 'd':
			d = min(r, d + arr[i])
	print(min(h, a, r, d))
main()


for t in range(1):
	n = int(input())
	st = input()
	cost = list(map(int, input().split()))
	arr = ['h', 'a', 'r', 'd']
	count = 0
	for i in range(n):
		if arr[count] == st[i]:
			count += 1
		if count == 4:
			break
	if count < 4:
		print(0)
		continue
	dp = [[999999999999999] * 4 for i in range(n + 1)]
	for i in range(n + 1):
		for j in range(4):
			if i == 0:
				dp[i] = [0, 0, 0, 0]
			elif st[i - 1] != arr[j]:
				dp[i][j] = dp[i - 1][j]
			elif j == 0:
				dp[i][j] = dp[i - 1][j] + cost[i - 1]
			else:
				dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j] + cost[i - 1])
	print(min(dp[-1]))


n = int(input())
s = input()
aa = []
dp0 = []
dp1 = []
dp2 = []
dp3 = []
inf = 1000000000000000
aa = list(map(int, input().split()))
for i in range(n + 1):
	dp0.append(inf)
	dp1.append(inf)
	dp2.append(inf)
	dp3.append(inf)
dp0[0] = 0
for i in range(n):
	if dp0[i] < inf:
		dp0[i + 1] = dp0[i]
		if s[i] == 'h':
			dp0[i + 1] += aa[i]
	if dp1[i] < inf:
		dp1[i + 1] = dp1[i]
		if s[i] == 'a':
			dp1[i + 1] += aa[i]
	if s[i] == 'h':
		dp1[i + 1] = min(dp1[i + 1], dp0[i])
	if dp2[i] < inf:
		dp2[i + 1] = dp2[i]
		if s[i] == 'r':
			dp2[i + 1] += aa[i]
	if s[i] == 'a':
		dp2[i + 1] = min(dp2[i + 1], dp1[i])
	if dp3[i] < inf:
		dp3[i + 1] = dp3[i]
		if s[i] == 'd':
			dp3[i + 1] += aa[i]
	if s[i] == 'r':
		dp3[i + 1] = min(dp3[i + 1], dp2[i])
answer = min(min(dp0[n], dp1[n]), min(dp2[n], dp3[n]))
print(answer)


n = int(input())
s = input()
d = {}
h = 0
a = 0
r = 0
d = 0
cost = [int(i) for i in input().split()]
for i in range(n):
	if s[i] == 'h':
		h += cost[i]
	if s[i] == 'a':
		a = min(a + cost[i], h)
	if s[i] == 'r':
		r = min(r + cost[i], a)
	if s[i] == 'd':
		d = min(d + cost[i], r)
print(d)


from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(10 ** 4)
PI = float('inf')
hard = 'hard'

def fn(pos, cur):
	if cur >= 4:
		return PI
	if pos == n:
		return 0 if cur < 4 else PI
	inc = exc = PI
	if cur < 4 and s[pos] == hard[cur]:
		inc = fn(pos + 1, cur + 1)
		exc = a[pos] + fn(pos + 1, cur)
	else:
		inc = fn(pos + 1, cur)
	return min(inc, exc)
for _ in range(1):
	n = int(stdin.readline())
	s = input()
	a = list(map(int, stdin.readline().split()))
	dp = [[PI for _ in range(4)] for _ in range(n + 1)]
	dp[n][0] = dp[n][1] = dp[n][2] = 0
	dp[n][3] = 0
	for i in range(n - 1, -1, -1):
		for j in range(4):
			if hard[j] == s[i]:
				dp[i][j] = min(PI if j + 1 >= 4 else dp[i + 1][j + 1], a[i] + dp[i + 1][j])
			else:
				dp[i][j] = dp[i + 1][j]
	print(min(dp[0]))


def f(a):
	s = 'hard'
	dp = [[float('inf')] * 5 for i in range(len(a) + 1)]
	dp[0][0] = 0
	for i in range(len(a)):
		for j in range(4):
			dp[i + 1][j + (s[j] == a[i][1])] = min(dp[i + 1][j + (s[j] == a[i][1])], dp[i][j])
			dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i][0])
	return min(dp[len(a)][:4])
n = int(input())
s = list(input())
cost = [*map(int, input().strip().split())]
a = [*zip(cost, s)]
print(f(a))


import sys
input = sys.stdin.readline
n = int(input())
s = input()
c = list(map(int, input().split()))
dp = [0] * 4
hard = 'hard'
se = set(['h', 'a', 'r', 'd'])
for j in range(n):
	if s[j] in se:
		if s[j] == hard[0]:
			dp[0] += c[j]
		else:
			for i in range(1, 4):
				if s[j] == hard[i]:
					dp[i] = min(dp[i - 1], dp[i] + c[j])
print(dp[-1])


n = int(input())
d = [0] * 4
k = input()
g = list(map(int, input().split()))
for i in range(n):
	if 'h' == k[i]:
		d[0] += g[i]
	if 'a' == k[i]:
		d[1] = min(d[0], d[1] + g[i])
	if 'r' == k[i]:
		d[2] = min(d[1], d[2] + g[i])
	if 'd' == k[i]:
		d[3] = min(d[2], d[3] + g[i])
print(min(d))


n = int(input())
(a, b, c, d) = (0, 0, 0, 0)
for (s, x) in zip(input(), map(int, input().split())):
	if s == 'h':
		a = a + x
	if s == 'a':
		b = min(a, b + x)
	if s == 'r':
		c = min(b, c + x)
	if s == 'd':
		d = min(c, d + x)
print(d)


from sys import stdin, stdout
nmbr = lambda : int(input())
lst = lambda : list(map(int, input().split()))
PI = float('inf')

def fn(pos, last):
	if pos < 0:
		return 0
	ans = PI
	if s[pos] == 'd':
		if last == 3:
			ans = a[pos] + fn(pos - 1, 3)
		elif last == 2:
			ans = fn(pos - 1, 2)
		elif last == 1:
			ans = fn(pos - 1, 1)
		elif last == 0:
			ans = fn(pos - 1, 0)
	elif s[pos] == 'r':
		if last == 3:
			ans = min(fn(pos - 1, 3), fn(pos - 1, 2))
		elif last == 2:
			ans = a[pos] + fn(pos - 1, 2)
		elif last == 1:
			ans = fn(pos - 1, 1)
		elif last == 0:
			ans = fn(pos - 1, 0)
	elif s[pos] == 'a':
		if last == 3:
			ans = fn(pos - 1, 3)
		elif last == 2:
			ans = min(fn(pos - 1, 2), fn(pos - 1, 1))
		elif last == 1:
			ans = a[pos] + fn(pos - 1, 1)
		elif last == 0:
			ans = fn(pos - 1, 0)
	elif s[pos] == 'h':
		if last == 3:
			ans = fn(pos - 1, 3)
		elif last == 2:
			ans = fn(pos - 1, 2)
		elif last == 1:
			ans = min(fn(pos - 1, 1), fn(pos - 1, 0))
		elif last == 0:
			ans = a[pos] + fn(pos - 1, 0)
	else:
		ans = fn(pos - 1, last)
	return ans
for _ in range(1):
	n = nmbr()
	s = input()
	a = lst()
	dp = [[PI for i in range(4)] for i in range(n + 1)]
	dp[0][0] = 0
	mp = {ch: v for (ch, v) in zip('hard', [1, 2, 3, 4])}
	for i in range(1, n + 1):
		for j in range(4):
			if mp.get(s[i - 1], -1) == j:
				dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1])
			elif mp.get(s[i - 1], -1) == j + 1:
				dp[i][j] = a[i - 1] + dp[i - 1][j]
			else:
				dp[i][j] = dp[i - 1][j]
	ans = PI
	for i in range(4):
		ans = min(ans, dp[n][i])
	print(ans)


def modify():
	s = ''
	a = []
	letters = set(['h', 'a', 'r', 'd'])
	for i in range(n):
		if s1[i] in letters:
			s += s1[i]
			a.append(a1[i])
	return (s, a)
n = int(input())
s1 = input()
a1 = list(map(int, input().split()))
(s, a) = modify()
n = len(s)
if n == 0:
	print(0)
	exit()
dp = [[10 ** 9 for i in range(4)] for j in range(n + 1)]
for i in range(4):
	dp[1][i] = 0
if s[0] == 'h':
	dp[1][0] += a[0]
for i in range(2, n + 1):
	if s[i - 1] == 'h':
		dp[i][0] = dp[i - 1][0] + a[i - 1]
		dp[i][1] = dp[i - 1][1]
		dp[i][2] = dp[i - 1][2]
		dp[i][3] = dp[i - 1][3]
	elif s[i - 1] == 'a':
		dp[i][1] = min(dp[i - 1][1] + a[i - 1], dp[i - 1][0])
		dp[i][0] = dp[i - 1][0]
		dp[i][2] = dp[i - 1][2]
		dp[i][3] = dp[i - 1][3]
	elif s[i - 1] == 'r':
		dp[i][2] = min(dp[i - 1][2] + a[i - 1], dp[i - 1][1])
		dp[i][1] = dp[i - 1][1]
		dp[i][0] = dp[i - 1][0]
		dp[i][3] = dp[i - 1][3]
	else:
		dp[i][3] = min(dp[i - 1][3] + a[i - 1], dp[i - 1][2])
		dp[i][1] = dp[i - 1][1]
		dp[i][0] = dp[i - 1][0]
		dp[i][2] = dp[i - 1][2]
print(dp[-1][3])


import sys
input = sys.stdin.readline
n = int(input())
S = input()
A = list(map(int, input().split()))
DP = [0] * 4
DP[0] = 0
for i in range(n):
	if S[i] == 'h':
		DP[0] += A[i]
	elif S[i] == 'a':
		DP[1] = min(DP[0], A[i] + DP[1])
	elif S[i] == 'r':
		DP[2] = min(DP[1], A[i] + DP[2])
	elif S[i] == 'd':
		DP[3] = min(DP[2], A[i] + DP[3])
print(min(DP))


n = int(input())
S = input()
A = [int(i) for i in input().split()]
INF = 10 ** 15
T = 'hard'
dp = [[INF] * (n + 1) for i in range(5)]
for i in range(5):
	dp[i][0] = 0
for (i, s) in enumerate(S):
	for (j, t) in enumerate('hard'):
		if s == t:
			dp[j][i + 1] = min(dp[j][i + 1], dp[j][i] + A[i])
			dp[j + 1][i + 1] = min(dp[j + 1][i + 1], dp[j][i])
		else:
			dp[j][i + 1] = min(dp[j][i + 1], dp[j][i])
print(min((dp[i][n] for i in range(4))))


n = int(input())
s = input()
data = input().split()
a = [0 for i in range(n + 1)]
d = [[0 for i in range(5)] for j in range(n + 1)]
c = ['w', 'h', 'a', 'r', 'd']
for i in range(1, n + 1):
	a[i] = int(data[i - 1])
s = '0' + s
for i in range(1, n + 1):
	if s[i] == c[1]:
		d[i][1] = d[i - 1][1] + a[i]
	else:
		d[i][1] = d[i - 1][1]
for j in range(2, 5):
	for i in range(1, n + 1):
		if s[i] == c[j]:
			d[i][j] = min(d[i - 1][j - 1], d[i - 1][j] + a[i])
		else:
			d[i][j] = d[i - 1][j]
print(d[n][4])


len_str = int(input())
string1 = input()
amb = list(map(int, input().split()))
dp = [[0 for i in range(4)] for j in range(len_str)]
hard = 'hard'
if string1[0] == 'h':
	dp[0][0] = amb[0]
for j in range(1, len_str):
	for i in range(0, 4):
		if string1[j] == hard[i] and i != 0:
			dp[j][i] = min(dp[j - 1][i - 1], dp[j - 1][i] + amb[j])
		elif string1[j] == hard[i] and i == 0:
			dp[j][i] = dp[j - 1][i] + amb[j]
		else:
			dp[j][i] = dp[j - 1][i]
print(dp[len_str - 1][3])


from sys import stdin, stdout
input = stdin.readline

def main():
	t = 1
	for z in range(t):
		n = int(input())
		s = input()
		ai = list(map(int, input().split()))
		dp = [0] * 4
		for i in range(n):
			for j in range(1, 4):
				dp[j] = min(dp[:j + 1])
			if s[i] == 'h':
				dp[1] = min(dp[1], dp[0])
				dp[0] += ai[i]
			elif s[i] == 'a':
				dp[2] = min(dp[2], dp[1])
				dp[1] = min(dp[0], dp[1] + ai[i])
			elif s[i] == 'r':
				dp[3] = min(dp[2], dp[3])
				dp[2] = min([dp[0], dp[1], dp[2] + ai[i]])
			elif s[i] == 'd':
				dp[3] = min([dp[0], dp[1], dp[2], dp[3] + ai[i]])
		print(min(dp))
main()


import sys
import math
input = sys.stdin.readline
from functools import cmp_to_key

def pi():
	return int(input())

def pl():
	return int(input(), 16)

def ti():
	return list(map(int, input().split()))

def ts():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())
mod = 998244353
f = []

def fact(n, m):
	global f
	f = [1 for i in range(n + 1)]
	f[0] = 1
	for i in range(1, n + 1):
		f[i] = f[i - 1] * i % m

def fast_mod_exp(a, b, m):
	res = 1
	while b > 0:
		if b & 1:
			res = res * a % m
		a = a * a % m
		b = b >> 1
	return res

def inverseMod(n, m):
	return fast_mod_exp(n, m - 2, m)

def ncr(n, r, m):
	if r == 0:
		return 1
	return f[n] * inverseMod(f[n - r], m) % m * inverseMod(f[r], m) % m

def main():
	XD()
dp = []

def D():
	[n, k] = ti()
	a = ti()
	a = sorted(a)
	cnt = [0 for i in range(n)]
	for i in range(n):
		c = 0
		for j in range(i, n):
			if a[j] - a[i] <= 5:
				c += 1
			else:
				break
		cnt[i] = c
	global dp
	dp = [[0 for j in range(k + 1)] for i in range(n + 1)]
	ans = 0
	for i in range(n):
		for j in range(k + 1):
			dp[i + 1][j] = max(dp[i + 1][j], dp[i][j])
			if j + 1 <= k:
				dp[i + cnt[i]][j + 1] = max(dp[i + cnt[i]][j + 1], dp[i][j] + cnt[i])
	print(dp[n][k])

def XD():
	(n, s) = (pi(), ts())
	a = ti()
	x = ['h', 'a', 'r', 'd']
	sub = [[0 for j in range(n)] for i in range(4)]
	dp = [[0 for j in range(n)] for i in range(4)]
	for i in range(4):
		for j in range(n):
			if i == 0 or j == 0:
				if x[i] == s[j]:
					sub[i][j] = 1
				else:
					sub[i][j] = sub[i][j - 1] if j != 0 else 0
			elif x[i] == s[j]:
				sub[i][j] = sub[i - 1][j - 1]
			else:
				sub[i][j] = sub[i][j - 1]
	for j in range(n):
		if s[j] == x[0]:
			dp[0][j] = dp[0][j - 1] + a[j] if j != 0 else a[j]
		else:
			dp[0][j] = dp[0][j - 1] if j != 0 else 0
	for i in range(4):
		for j in range(n):
			dp[i][j] = 9999999999999999 if dp[i][j] == 0 else dp[i][j]
	for i in range(1, 4):
		for j in range(n):
			if j == 0:
				dp[i][j] = 0
				continue
			if s[j] == x[i]:
				if sub[i - 1][j - 1] == 1:
					if sub[i][j - 1] == 1:
						dp[i][j] = min(dp[i - 1][j - 1], dp[i][j - 1] + a[j])
					else:
						dp[i][j] = min(dp[i - 1][j - 1], a[j])
				else:
					dp[i][j] = 0
			else:
				dp[i][j] = dp[i][j - 1]
	print(dp[3][n - 1] if dp[3][n - 1] != 9999999999999999 else 0)
main()


n = int(input())
a = input()
w = list(map(int, input().split()))
dp = [[0] * 4 for _ in range(n + 1)]
if a[-1] == 'd':
	dp[-2][3] = w[-1]
l = [n] * 4
d = {'h': 0, 'a': 1, 'r': 2, 'd': 3}
for i in range(n - 2, -1, -1):
	ind = d.get(a[i], -1)
	for j in range(4):
		dp[i] = dp[i + 1]
		if j == ind:
			if j <= 2:
				dp[i][j] = min(w[i] + dp[l[j]][j], dp[i + 1][j + 1])
			else:
				dp[i][j] = w[i] + dp[i + 1][j]
			l[j] = i
print(dp[0][0])


n = int(input())
s = input()
lst = [int(x) for x in input().split()]
dp = []
for i in range(0, n + 1):
	f = []
	for j in range(0, 4):
		f.append(0)
	dp.append(f)
ans = 100000000000000000000000000
for i in range(0, n):
	if s[i] == 'h':
		dp[i + 1][1] = dp[i][1]
		dp[i + 1][0] = dp[i][0] + lst[i]
		dp[i + 1][2] = dp[i][2]
		dp[i + 1][3] = dp[i][3]
	elif s[i] == 'a':
		dp[i + 1][0] = dp[i][0]
		dp[i + 1][1] = min(dp[i][1] + lst[i], dp[i][0])
		dp[i + 1][2] = dp[i][2]
		dp[i + 1][3] = dp[i][3]
	elif s[i] == 'r':
		dp[i + 1][0] = dp[i][0]
		dp[i + 1][1] = dp[i][1]
		dp[i + 1][2] = min(min(dp[i][2] + lst[i], dp[i][1]), dp[i][0])
		dp[i + 1][3] = dp[i][3]
	elif s[i] == 'd':
		dp[i + 1][0] = dp[i][0]
		dp[i + 1][1] = dp[i][1]
		dp[i + 1][2] = dp[i][2]
		dp[i + 1][3] = min(min(min(dp[i][3] + lst[i], dp[i][2]), dp[i][1]), dp[i][0])
	else:
		dp[i + 1][0] = dp[i][0]
		dp[i + 1][1] = dp[i][1]
		dp[i + 1][2] = dp[i][2]
		dp[i + 1][3] = dp[i][3]
ans = min(ans, min(min(min(dp[-1][0], dp[-1][1]), dp[-1][2]), dp[-1][3]))
print(ans)


n = int(input())
s = list(input())
a = list(map(int, input().split()))
dp = [1e+20 for _ in range(4)]
dp[0] = 0
for i in range(n):
	if s[i] == 'h':
		dp[1] = min(dp[0], dp[1])
		dp[0] += a[i]
	elif s[i] == 'a':
		dp[2] = min(dp[1], dp[2])
		dp[1] += a[i]
	elif s[i] == 'r':
		dp[3] = min(dp[2], dp[3])
		dp[2] += a[i]
	elif s[i] == 'd':
		dp[3] += a[i]
ans = min(dp)
print(ans)


from math import inf
n = int(input())
s = input()
a = [int(i) for i in input().split()]
dp = [[inf] * 5 for i in range(n + 1)]
dp[0][0] = 0
for (i, c) in enumerate(s):
	for (j, h) in enumerate('hard'):
		ji = j + int(h == c)
		dp[i + 1][ji] = min(dp[i + 1][ji], dp[i][j])
		dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + a[i])
print(min(dp[n][:-1]))


n = int(input())
s = input()
a = [int(o) for o in input().split()]
dp = [[0 for i in range(5)] for j in range(n + 1)]
for i in range(n + 1):
	dp[i][4] = float('inf')
word = list('hard')
di = {'h': 0, 'a': 1, 'r': 2, 'd': 3}
for i in range(1, n + 1):
	dp[i][0] = dp[i - 1][0]
	dp[i][1] = dp[i - 1][1]
	dp[i][2] = dp[i - 1][2]
	dp[i][3] = dp[i - 1][3]
	if s[i - 1] in word:
		dp[i][di[s[i - 1]]] = min(a[i - 1] + dp[i][di[s[i - 1]]], dp[i][di[s[i - 1]] - 1])
print(min(dp[n]))


n = int(input())
s = str(input())
x = list(map(int, input().split()))
dp = [[0] * 4 for i in range(n + 1)]
for i in range(0, n):
	dp[i + 1][0] = dp[i][0] + x[i] * (s[i] == 'h')
	if s[i] == 'h':
		dp[i + 1][1] = min(dp[i][0], dp[i][1])
	else:
		dp[i + 1][1] = dp[i][1] + (s[i] == 'a') * x[i]
	if s[i] == 'a':
		dp[i + 1][2] = min(dp[i][1], dp[i][2])
	else:
		dp[i + 1][2] = dp[i][2] + (s[i] == 'r') * x[i]
	if s[i] == 'r':
		dp[i + 1][3] = min(dp[i][2], dp[i][3])
	else:
		dp[i + 1][3] = dp[i][3] + (s[i] == 'd') * x[i]
print(min(dp[n]))


n = int(input())
s = input()
A = list(map(int, input().split()))
t = 'hard'
dph = [0] * (n + 1)
for i in range(n):
	dph[i + 1] = dph[i] + A[i] * (s[i] == 'h')
dpha = [0] * (n + 1)
for i in range(n):
	if s[i] != 'a':
		dpha[i + 1] = dpha[i]
	else:
		dpha[i + 1] = min(dpha[i] + A[i], dph[i])
dphar = [0] * (n + 1)
for i in range(n):
	if s[i] != 'r':
		dphar[i + 1] = dphar[i]
	else:
		dphar[i + 1] = min(dphar[i] + A[i], dpha[i])
dphard = [0] * (n + 1)
for i in range(n):
	if s[i] != 'd':
		dphard[i + 1] = dphard[i]
	else:
		dphard[i + 1] = min(dphard[i] + A[i], dphar[i])
print(dphard[-1])


n = int(input())
s = list(input())
u = list(map(int, input().split()))
d = []
lt = ['h', 'a', 'r', 'd']
for i in range(4):
	d.append([0] * n)
if s[0] == lt[0]:
	d[0][0] = u[0]
for i in range(1, n):
	if s[i] == lt[0]:
		d[0][i] = d[0][i - 1] + u[i]
		d[1][i] = d[1][i - 1]
		d[2][i] = d[2][i - 1]
		d[3][i] = d[3][i - 1]
	elif s[i] == lt[1]:
		d[1][i] = min(d[1][i - 1] + u[i], d[0][i - 1])
		d[0][i] = d[0][i - 1]
		d[2][i] = d[2][i - 1]
		d[3][i] = d[3][i - 1]
	elif s[i] == lt[2]:
		d[2][i] = min(d[2][i - 1] + u[i], d[1][i - 1])
		d[0][i] = d[0][i - 1]
		d[1][i] = d[1][i - 1]
		d[3][i] = d[3][i - 1]
	elif s[i] == lt[3]:
		d[3][i] = min(d[3][i - 1] + u[i], d[2][i - 1])
		d[0][i] = d[0][i - 1]
		d[1][i] = d[1][i - 1]
		d[2][i] = d[2][i - 1]
	else:
		d[0][i] = d[0][i - 1]
		d[1][i] = d[1][i - 1]
		d[2][i] = d[2][i - 1]
		d[3][i] = d[3][i - 1]
print(min(d[0][-1], d[1][-1], d[2][-1], d[3][-1]))