#include <bits/stdc++.h>
using namespace std;
long long int inf = 1e14;
long long int mod = 998244353;
char en = '\n';
char arr[1000005];
long long int dp[1000005][5];
char desired[4] = {'h', 'a', 'r', 'd'};
long long int arr2[1000005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  for (long long int i = 1; i <= n; i++) arr[i] = s[i - 1];
  for (long long int i = 1; i <= n; i++) cin >> arr2[i];
  for (long long int i = 0; i <= n; i++) {
    for (long long int j = 0; j < 4; j++) dp[i][j] = inf;
  }
  dp[0][0] = 0;
  for (long long int i = 0; i < n; i++) {
    for (long long int j = 0; j < 4; j++) {
      if (arr[i + 1] == desired[j]) {
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + arr2[i + 1]);
        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]);
      }
    }
  }
  long long int res = inf;
  for (long long int i = 0; i < 4; i++) res = min(res, dp[n][i]);
  cout << res << en;
  return 0;
}
