You are on page 1of 3

#include<iostream>

#include<stdio.h>
using namespace std;
struct tch{int ti,si;
tch *nxt;
tch *lst;
};
tch **a=new tch*[100001];
tch **b=new tch*[100001];int k;
void fastscan(int &number)
{
//variable to indicate sign of input number
bool negative = false;
register int c;

number = 0;

// extract current character from buffer


c = getchar();
if (c=='-')
{
// number is negative
negative = true;

// extract the next character from the buffer


c = getchar();
}

// Keep on extracting characters if they are integers


// i.e ASCII Value lies from '0'(48) to '9' (57)
for (; (c>47 && c<58); c=getchar())
number = number *10 + c - 48;

// if scanned input has a negative sign, negate the


// value of the input number
if (negative)
number *= -1;
}

int heapify(int n)
{if(n==1)
return 0;
int p;
if(n%2==0)
{
p=n/2;
if(b[p]->si<b[n]->si)
{swap(b[p],b[n]);
n=p;
}
else return 0;
}
else{p=(n-1)/2;
if(b[p]->si<b[n]->si)
{swap(b[n],b[p]);
n=p;
}else return 0;
}
heapify(n);
}
int swp(int i)
{if(i*2>k)
return 0;
int f=0;
if(i*2+1>k)
{
if(b[i]->si<b[i*2]->si)
{swap(b[i*2],b[i]);
i=i*2;
return 0;
}
else
return 0;
}
else if(b[i*2]->si>b[i*2+1]->si&&b[i*2]->si>b[i]->si)
{swap(b[i*2],b[i]);
i=i*2;
f=1;
}
else if(b[i*2+1]->si>b[i]->si){swap(b[i*2+1],b[i]);
i=i*2+1;
f=1;
}
if(f==0)
return 0;
swp(i);
}

int main()
{ int t;
long long h[10],tt=0;
fastscan(t);
while(t--)
{int n,d;
k=0;
for(int i=1;i<=100000;i++)
a[i]=NULL;
long long sad=0;
fastscan(n);fastscan(d);
for(int i=0;i<n;i++)
{int t1,t2,t3;
fastscan(t1);fastscan(t2);
fastscan(t3);if(a[t1]==NULL)
{
tch *cur=new tch;

cur->ti=t2;
cur->si=t3;
cur->nxt=NULL;
cur->lst=cur;
a[t1]=cur;
}
else
{tch *cur=new tch;

cur->ti=t2;
cur->si=t3;
cur->nxt=NULL;
(a[t1]->lst)->nxt=cur;
a[t1]->lst=cur;

}}
int t1=0;

for(int i=1;i<=d;i++)
if(a[i]!=NULL){while(a[i]!=NULL)
{if(t1!=0)
{int avail=i-t1;
while(k>0&&avail>=(b[1]->ti))
{
avail=avail-(b[1]->ti);
b[1]=b[k];
--k;
swp(1);
}
if(k!=0)
{
(b[1]->ti)=(b[1]->ti)-avail;
}
}
else
t1=i;
b[k+1]=a[i];
++k;
heapify(k);
t1=i;
a[i]=(a[i]->nxt);
}

}
{int avail=d-t1+1;
while(avail>=(b[1]->ti)&&k>0)
{
avail=avail-(b[1]->ti);
b[1]=b[k];
--k;
swp(1);
}
if(k!=0)
{
(b[1]->ti)=(b[1]->ti)-avail;
}
}
for(int i=1;i<=k;i++)
{
sad=sad+(long long)(b[i]->si)*(long long)(b[i]->ti);
}
printf("%lld\n",sad);

}
}

You might also like