记录编号 158174 评测结果 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
题目名称 [APIO 2012] 派遣 最终得分 100
用户昵称 GravatarAsm.Def 是否通过 通过
代码语言 C++ 运行时间 0.593 s
提交时间 2015-04-13 08:49:31 内存使用 8.87 MiB
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <algorithm>
#include <cmath>
using namespace std;
char * ptr=(char *)malloc(5000000);
#define getc() (*(ptr++))
#define SetFile(x) (freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout))
template<class T>inline void getd(T &x){
    char ch = getc();bool neg = false;
    while(!isdigit(ch) && ch != '-')ch = getc();
    if(ch == '-')ch = getc(), neg = true;
    x = ch - '0';
    while(isdigit(ch = getc()))x = x * 10 - '0' + ch;
    if(neg)x = -x;
}
/***********************************************************************/
const int maxn = 100003;
typedef long long LL;
int N, M, p[maxn], leader[maxn];
LL Ans = 0;
struct Heap *Nil;
struct Heap{//最大堆,维护和
	int val, size, dis;
	LL sum;
	Heap *ls, *rs;
	inline void init(int v){sum = val = v;size = 1;dis = 0;ls = rs = Nil;}
	friend Heap *Merge(Heap *a, Heap *b){
		if(a->val < b->val)swap(a, b);
		if(b == Nil)return a;
		a->sum += b->sum;
		a->size += b->size;
		a->rs = Merge(a->rs, b);
		if(a->ls->dis < a->rs->dis)swap(a->ls, a->rs);
		a->dis = a->rs->dis + 1;
		return a;
	}
	inline Heap *pop(){return Merge(ls, rs);}
}pool[maxn], *node[maxn];
inline void work(){
	Nil = pool;Nil->dis = -1;
	getd(N), getd(M);
	int i, v;
	Heap *it, **i2, *t;*node = pool;
	for(i = 1,it = pool+1;i <= N;++i, ++it){
		node[i] = it;
		getd(p[i]), getd(v), getd(leader[i]);
		it->init(v);
	}
	for(i = N,i2 = node + N;i;--i, --i2){
		t = *i2;
		while(t->sum > M)t = t->pop();
		Ans = max(Ans, (LL)leader[i] * t->size);
		node[p[i]] = Merge(t, node[p[i]]);
	}
	printf("%lld\n", Ans);
}

int main(){
	#ifdef DEBUG
	freopen("test.txt", "r", stdin);
	#else
	SetFile(dispatching);
	#endif
	fread(ptr,1,5000000,stdin);
	work();
 
#ifdef DEBUG
    printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
#endif
    return 0;
}