#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
using ll = long long;
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
  os << "[ ";
  for (auto e : v) os << e << " ";
  return os << "]";
}
template <typename T, typename V>
ostream& operator<<(ostream& os, set<T, V> v) {
  os << "{ ";
  for (auto e : v) os << e << " ";
  return os << "}";
}
template <typename T, typename V, typename S>
ostream& operator<<(ostream& os, map<T, V, S> v) {
  os << "{ ";
  for (auto e : v) os << e << " ";
  return os << "}";
}
template <typename T, typename V>
ostream& operator<<(ostream& os, pair<T, V> x) {
  return os << "( " << x.first << ", " << x.second << " )";
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
  for (auto& e : v) is >> e;
  return is;
}
template <typename T, typename V>
istream& operator>>(istream& is, pair<T, V>& x) {
  return is >> x.first >> x.second;
}
void solve();
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
}
void solve() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> a(n);
  cin >> a;
  const string t = "hard";
  vector<vector<ll>> dp(n, vector<ll>(t.size(), 0));
  for (int j = 0; j < n; ++j) {
    if (s[j] == t[0]) {
      dp[j][0] = a[j];
    }
    if (j > 0) {
      dp[j][0] += dp[j - 1][0];
    }
  }
  for (int i = 1; i < t.size(); ++i) {
    dp[0][i] = 0;
    for (int j = 1; j < n; ++j) {
      if (s[j] == t[i]) {
        dp[j][i] = min(dp[j - 1][i - 1], dp[j - 1][i] + 1LL * a[j]);
      } else {
        dp[j][i] = dp[j - 1][i];
      }
    }
  }
  cout << dp[n - 1][t.size() - 1] << "\n";
}
