#树状数组# Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2) C. Fountains

题目链接

C. Fountains

time limit per test  2 seconds

memory limit per test  256 megabytes

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 bi and pi (1 ≤ bi, pi ≤ 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

题目描述:

要求建两个喷泉,现在有n个可以选的喷泉,和起始有硬币c个钻石d个,每一个喷泉的漂亮度和价格已经给出,求出他能建的喷泉的方法中最大的漂亮度。

Solution:

要建两个喷泉,一共就三种情况:

  • 选两个用硬币买的喷泉
  • 选两个用钻石买的喷泉
  • 选一个用硬币买的喷泉再选一个用钻石买的喷泉

利用树状数组存下价格为p的喷泉的价值,那么就能快速求出价格为1~p的最大价值。

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#define mst(a, b) memset(a, b, sizeof(a))
#define rush() int T; scanf("%d", &T); while(T--)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MaxN = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

//priority_queue<int, vector<int>, greater<int> > que;
int n, c, d;
int a[MaxN], b[MaxN]; //a数组为硬币,b数组为钻石

int lowbit(int x) { return x & (-x); }

void add(int p, int x, int *t) { //update
	for(int i = p; i <= MaxN; i += lowbit(i)) t[i] = max(t[i], x);
}

int get_max(int p, int *t) { //维护前缀最大值
	int res = 0;
	for(int i = p; i > 0; i -= lowbit(i)) res = max(res, t[i]);
	return res;
}

int main(){
	scanf("%d %d %d", &n, &c, &d);
	int ans = 0;
	for(int i = 1; i <= n; i++) {
		int v, w; char ch[5]; //v是价值,w是花费
		scanf("%d %d %s", &v, &w, ch);
		int maxx = 0;
		if(ch[0] == 'C') { //在取当前的硬币的情况下
			if(w > c) continue; //当前硬币花费大于可花费钱数,即当前硬币不可取
			maxx = get_max(d, b); //再取一个可取的价值最大的钻石
			maxx = max(maxx, get_max(c - w, a)); //再取一个前缀最大价值的硬币 与 之前取钻石得到的maxx 取最大值
			add(w, v, a); //update
		} else { //取当前钻石的情况下
			if(w > d) continue;
			maxx = get_max(c, a); //再取一个硬币
			maxx = max(maxx, get_max(d - w, b)); //再取一个钻石取max
			add(w, v, b);
		}
		if(maxx) ans = max(maxx + v, ans); //得到再取硬币还是钻石后的最大价值上 加上当前要取的硬币或者钻石的价值
	}
	printf("%d\n", ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Jasmineaha/article/details/81124648