#include using namespace std; using lint = long long; using pint = pair; using plint = pair; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define ALL(x) (x).begin(), (x).end() #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) template void ndarray(vector& vec, const V& val, int len) { vec.assign(len, val); } template void ndarray(vector& vec, const V& val, int len, Args... args) { vec.resize(len), for_each(begin(vec), end(vec), [&](T& v) { ndarray(v, val, args...); }); } template bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } template bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; } template pair operator+(const pair &l, const pair &r) { return make_pair(l.first + r.first, l.second + r.second); } template pair operator-(const pair &l, const pair &r) { return make_pair(l.first - r.first, l.second - r.second); } template vector srtunq(vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } template istream &operator>>(istream &is, vector &vec) { for (auto &v : vec) is >> v; return is; } template ostream &operator<<(ostream &os, const vector &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; } #if __cplusplus >= 201703L template istream &operator>>(istream &is, tuple &tpl) { std::apply([&is](auto &&... args) { ((is >> args), ...);}, tpl); return is; } template ostream &operator<<(ostream &os, const tuple &tpl) { std::apply([&os](auto &&... args) { ((os << args << ','), ...);}, tpl); return os; } #endif template ostream &operator<<(ostream &os, const deque &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; } template ostream &operator<<(ostream &os, const set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_set &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_multiset &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const pair &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; } template ostream &operator<<(ostream &os, const map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } template ostream &operator<<(ostream &os, const unordered_map &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; } #ifdef HITONANODE_LOCAL #define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl #else #define dbg(x) {} #endif constexpr int B = 501; int main() { int N; string S; cin >> N >> S; vector V(N); cin >> V; constexpr lint INF = 1e18; vector> dp[B]; REP(i3, B) { int y = clamp((N - i3 * 3) / 2, 0, B - i3 - 1); dp[i3].resize(y + 1); REP(i2, dp[i3].size()) { int y = N - i3 * 3 - i2 * 2; y = clamp(y, 0, B - i3 - i2 - 1); dp[i3][i2].resize(y + 1, -INF); } } dp[0][0][0] = 0; REP(i, N) { if (S[i] == 'y') { REP(i3, B) REP(i2, dp[i3].size()) IREP(i1, dp[i3][i2].size() - 1) { chmax(dp[i3][i2][i1 + 1], dp[i3][i2][i1] + V[i]); } } if (S[i] == 'u') { REP(i3, B) IREP(i2, int(dp[i3].size()) - 1) REP(i1, dp[i3][i2].size() - 1) { chmax(dp[i3][i2 + 1][i1], dp[i3][i2][i1 + 1] + V[i]); } } if (S[i] == 'k') { IREP(i3, B - 1) REP(i2, int(dp[i3 + 1].size()) - 1) REP(i1, dp[i3 + 1][i2].size()) { chmax(dp[i3 + 1][i2][i1], dp[i3][i2 + 1][i1] + V[i]); } } if (S[i] == 'i') { REP(i3, B - 1) REP(i2, int(dp[i3 + 1].size()) - 1) REP(i1, dp[i3 + 1][i2].size()) { chmax(dp[i3][i2][i1], dp[i3 + 1][i2][i1] + V[i]); } } } cout << dp[0][0][0] << '\n'; }