#include <bits/stdc++.h>
using namespace std;
const int P = 4;
const long long INF = 998244353000000ll;
class Application {
 private:
 public:
  void run() {
    int n;
    cin >> n;
    string s;
    vector<long long> a(n);
    vector<vector<long long>> dp(n + 1);
    for (int i = 0; i < n + 1; ++i) dp[i] = {0, INF, INF, INF};
    cin >> s;
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) {
      switch (s[i]) {
        case 'h':
          dp[i + 1][0] = dp[i][0] + a[i];
          dp[i + 1][1] = min(dp[i][0], dp[i][1]);
          for (int j = 0; j < P; j++)
            if (j != 0 && j != 1) dp[i + 1][j] = dp[i][j];
          break;
        case 'a':
          dp[i + 1][1] = dp[i][1] + a[i];
          dp[i + 1][2] = min(dp[i][1], dp[i][2]);
          for (int j = 0; j < P; j++)
            if (j != 1 && j != 2) dp[i + 1][j] = dp[i][j];
          break;
        case 'r':
          dp[i + 1][2] = dp[i][2] + a[i];
          dp[i + 1][3] = min(dp[i][2], dp[i][3]);
          for (int j = 0; j < P; j++)
            if (j != 2 && j != 3) dp[i + 1][j] = dp[i][j];
          break;
        case 'd':
          dp[i + 1][3] = dp[i][3] + a[i];
          for (int j = 0; j < P; j++)
            if (j != 3) dp[i + 1][j] = dp[i][j];
          break;
        default:
          for (int j = 0; j < P; j++) dp[i + 1][j] = dp[i][j];
          break;
      }
    }
    cout << *min_element(begin(dp[n]), end(dp[n]));
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Application app;
  app.run();
  return 0;
}
