#include <bits/stdc++.h>
using namespace std;
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& v);
template <class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T, size_t N>
ostream& operator<<(ostream& os, const array<T, N>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const set<T1, T2>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multiset<T1, T2>& v) {
  bool f = 1;
  os << "[";
  for (auto& i : v) {
    if (!f) os << ", ";
    os << i;
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2, class T3>
ostream& operator<<(ostream& os, const map<T1, T2, T3>& v) {
  bool f = 1;
  os << "[";
  for (auto& ii : v) {
    if (!f) os << ", ";
    os << "(" << ii.first << " -> " << ii.second << ") ";
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, const multimap<T1, T2>& v) {
  bool f = 1;
  os << "[";
  for (auto& ii : v) {
    if (!f) os << ", ";
    os << "(" << ii.first << " -> " << ii.second << ") ";
    f = 0;
  }
  return os << "]";
}
template <class T1, class T2>
ostream& operator<<(ostream& os, priority_queue<T1, T2> v) {
  bool f = 1;
  os << "[";
  while (!v.empty()) {
    auto x = v.top();
    v.pop();
    if (!f) os << ", ";
    f = 0;
    os << x;
  }
  return os << "]";
}
template <class T1, class T2>
void printarray(const T1& a, T2 sz, T2 beg = 0) {
  for (T2 i = beg; i < sz; i++) cout << a[i] << " ";
  cout << endl;
}
void cio() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
}
const long long mod = 998244353;
int N;
char s[100500];
long long dp[100500][7];
long long p[100500];
int main() {
  scanf("%d", &N);
  scanf("%s", s + 1);
  for (int i = 1; i <= N; i++) scanf("%lld", p + i);
  long long sum = 0;
  for (int i = 1; i <= N; i++) sum += p[i];
  for (int i = 0; i < N + 1; i++)
    for (int j = 0; j <= 4; j++) dp[i][j] = -1e16;
  dp[0][0] = 0;
  for (int n = 1; n <= N; n++) {
    for (int k = 0; k < 4; k++) dp[n][k] = max(dp[n][k], dp[n - 1][k]);
    if (s[n] != 'h') dp[n][0] = max(dp[n][0], dp[n - 1][0] + p[n]);
    if (s[n] == 'h')
      dp[n][1] = max({dp[n][1], dp[n - 1][0] + p[n], dp[n - 1][1] + p[n]});
    if (s[n] != 'a') dp[n][1] = max(dp[n][1], dp[n - 1][1] + p[n]);
    if (s[n] == 'a')
      dp[n][2] = max({dp[n][2], dp[n - 1][1] + p[n], dp[n - 1][2] + p[n]});
    if (s[n] != 'r') dp[n][2] = max(dp[n][2], dp[n - 1][2] + p[n]);
    if (s[n] == 'r')
      dp[n][3] = max({dp[n][3], dp[n - 1][2] + p[n], dp[n - 1][3] + p[n]});
    if (s[n] != 'd') dp[n][3] = max(dp[n][3], dp[n - 1][3] + p[n]);
  }
  long long mx = 0;
  for (int i = 0; i < 4; i++) mx = max(mx, dp[N][i]);
  printf("%lld\n", sum - mx);
}
