Fountains

Arkady plays Gardenscapes a lot. Arkady wants to build two new fountains. There are n available fountains, for each fountain its beauty and cost are known. There are two types of money in the game: coins and diamonds, so each fountain cost can be either in coins or diamonds. No money changes between the types are allowed.

Help Arkady to find two fountains with maximum total beauty so that he can buy both at the same time.

Input

The first line contains three integers nc and d (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 000) — the number of fountains, the number of coins and diamonds Arkady has.

The next n lines describe fountains. Each of these lines contain two integers b i and p i (1 ≤ b i, p i ≤ 100 000) — the beauty and the cost of the i-th fountain, and then a letter “C” or “D”, describing in which type of money is the cost of fountain i: in coins or in diamonds, respectively.

Output

Print the maximum total beauty of exactly two fountains Arkady can build. If he can’t build two fountains, print 0.

Examples

input

3 7 6
10 8 C
4 3 C
5 6 D

output

9

input

2 4 5
2 5 C
2 1 D

output

0

input

3 10 10
5 5 C
5 5 C
10 11 D

output

10

Note

In the first example Arkady should build the second fountain with beauty 4, which costs 3 coins. The first fountain he can’t build because he don’t have enough coins. Also Arkady should build the third fountain with beauty 5 which costs 6 diamonds. Thus the total beauty of built fountains is 9.

In the second example there are two fountains, but Arkady can’t build both of them, because he needs 5 coins for the first fountain, and Arkady has only 4 coins.

Solution:

#include <bits/stdc++.h>

using namespace std;

const int N = 1234567;

int pref[N], a[N], b[N];
char c[N];

int solve(vector < pair <int, int> > z, int b) {
sort(z.begin(), z.end());
int cnt = z.size();
if (cnt < 2) {
    return 0;
  }
  pref[0] = 0;
  for (int i = 0; i < cnt; i++) {
    pref[i + 1] = max(pref[i], z[i].second);
  }
  int i = 0;
  int res = 0;
  for (int j = cnt - 1; j >= 0; j--) {
while (i < j && z[i].first + z[j].first <= b) {
      i++;
    }
    i = min(i, j);
    if (i > 0) {
res = max(res, pref[i] + z[j].second);
}
}
return res;
}

int main() {
int n, C, D;
scanf("%d %d %d", &n, &C, &D);
for (int i = 0; i < n; i++) {
    scanf("%d %d", a + i, b + i);
    c[i] = getchar();
    while (c[i] != 'C' && c[i] != 'D') {
      c[i] = getchar();
    }
  }
  int ans = 0;
  {
    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
      if (c[i] == 'C' && b[i] <= C) {
        x = max(x, a[i]);
      }
    }
    for (int i = 0; i < n; i++) {
      if (c[i] == 'D' && b[i] <= D) {
        y = max(y, a[i]);
      }
    }
    if (x > 0 && y > 0) {
ans = max(ans, x + y);
}
}
{
vector < pair <int, int> > z;
for (int i = 0; i < n; i++) {
      if (c[i] == 'C') {
        z.push_back(make_pair(b[i], a[i]));
      }
    }
    ans = max(ans, solve(z, C));
  }
  {
    vector < pair <int, int> > z;
for (int i = 0; i < n; i++) {
      if (c[i] == 'D') {
        z.push_back(make_pair(b[i], a[i]));
      }
    }
    ans = max(ans, solve(z, D));
  }
  printf("%d\n", ans);
  return 0;
}