#include <bits/stdc++.h>
inline int read() {
  char c = getchar();
  int n = 0;
  bool f = false;
  while (c < '0' || c > '9') {
    if (c == '-') {
      f = true;
    }
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    n = (n << 1) + (n << 3) + (c & 15);
    c = getchar();
  }
  return f ? -n : n;
}
const int maxN = 100005;
int n;
long long sum, val[maxN], f[4][maxN];
std::string s;
int main() {
  std::cin >> n >> s;
  s = " " + s;
  for (int i = 1; i <= n; i++) {
    val[i] = read();
    sum += val[i];
  }
  memset(f, 128, sizeof(f));
  f[0][0] = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'h') {
      for (int j = 0; j <= 3; j++) {
        f[j][i] = f[j][i - 1];
      }
      for (int j = 0; j <= 3; j++) {
        if (j == 0) {
          continue;
        }
        f[j][i] = std::max(f[j][i], f[j][i - 1] + val[i]);
        f[j][i] = std::max(f[j][i], f[j == 1 ? j - 1 : j][i - 1] + val[i]);
      }
    } else if (s[i] == 'a') {
      for (int j = 0; j <= 3; j++) {
        f[j][i] = f[j][i - 1];
      }
      for (int j = 0; j <= 3; j++) {
        if (j == 1) {
          continue;
        }
        f[j][i] = std::max(f[j][i], f[j][i - 1] + val[i]);
        f[j][i] = std::max(f[j][i], f[j == 2 ? j - 1 : j][i - 1] + val[i]);
      }
    } else if (s[i] == 'r') {
      for (int j = 0; j <= 3; j++) {
        f[j][i] = f[j][i - 1];
      }
      for (int j = 0; j <= 3; j++) {
        if (j == 2) {
          continue;
        }
        f[j][i] = std::max(f[j][i], f[j][i - 1] + val[i]);
        f[j][i] = std::max(f[j][i], f[j == 3 ? j - 1 : j][i - 1] + val[i]);
      }
    } else if (s[i] == 'd') {
      for (int j = 0; j <= 3; j++) {
        f[j][i] = f[j][i - 1];
      }
      for (int j = 0; j <= 3; j++) {
        if (j == 3) {
          continue;
        }
        f[j][i] = std::max(f[j][i], f[j][i - 1] + val[i]);
      }
    } else {
      for (int j = 0; j <= 3; j++) {
        f[j][i] = f[j][i - 1] + val[i];
      }
    }
  }
  printf("%I64d\n", sum - std::max(std::max(f[0][n], f[1][n]),
                                   std::max(f[2][n], f[3][n])));
  return 0;
}
