#include #include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) #define all(x) begin(x), end(x) template bool chmin(T& x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T& x, T y) { return x < y ? (x = y, true) : false; } struct IOST { IOST() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20); } } IOST; void solve() { int n; cin >> n; vector a(n); rep(i, 0, n) cin >> a[i]; vector d(3); rep(i, 0, n) { string s; cin >> s; if (s == "add") d[0]++; else if (s == "max") d[1]++; else d[2]++; } sort(a.begin(), a.end()); ll t = 0; if (d[0] + d[1] > 0) chmax(t, a[d[0] + d[1] - 1]); if (d[0] > 1) { rep(i, 0, d[0] - 1) t += a[i]; } if (d[2] > 0) chmin(t, a[n - d[2]]); cout << t << "\n"; } int main() { int t = 1; // cin >> t; rep(i, 0, t) solve(); }