#include using namespace std; using ll = long long; using ld = long double; using vll = vector; using vvll = vector>; using vvvll = vector>>; using vvvvll = vector>>>; using vpll = vector>; using vs = vector; using vb = vector; using vvb = vector>; using pll = pair; #define REP(i, n) for (long long i = 0; i < n; i++) #define REP2(i, n, m) for (long long i = n; i < m; i++) #define REPv(v) for (auto itr = v.begin(); itr != v.end(); itr++) #define REPIN(v) \ for (long long i = 0; i < (long long)v.size(); i++) cin >> v[i]; #define been(vec) vec.begin(), vec.end() #define LIN \ = []() { \ long long x; \ cin >> x; \ return x; \ }() #define SIN \ = []() { \ string x; \ cin >> x; \ return x; \ }() #define SPNL(i, SIZE) (i + 1 == SIZE ? '\n' : ' ') ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; } ll lcm(ll x, ll y) { return x / gcd(x, y) * y; } bool chmin(ll &a, const ll &b) { return (a > b && ((a = b) | 1)); } bool chmax(ll &a, const ll &b) { return (a < b && ((a = b) | 1)); } using namespace std; int main() { ll n LIN, x LIN, y LIN; ll nd = min(x + y, n); vvll pc(n, {0, 0}); REP(i, n) { char tmp; cin >> pc[i][0] >> tmp; pc[i][1] = tmp; } // (x+y)k ll roomA = 0; vvll ide(nd, vll(2, 0)); vll d(nd); REP(i, nd) { for (ll j = 0; i + j * (x + y) < n; j++) { ll I = i + j * (x + y); if (pc[I][1] == ll('A')) ide[i][0] += pc[I][0]; else ide[i][1] += pc[I][0]; } d[i] = ide[i][1] - ide[i][0]; roomA += ide[i][0]; } sort(been(d), [](ll a, ll b) { return a > b; }); REP(i, nd) { if (i < nd - x) roomA += d[i]; else if (i < y && d[i] > 0) roomA += d[i]; } cout << roomA << endl; return 0; }