/*Problem Catagory:Dynamic Programming*/ #include #include #define LIMIT 2000 #define LARGE(a, b) (a > b ? a : b) #define rep(a,b) for(i=a;i=b;j--) #define ULL unsigned long long ULL result[LIMIT+1],TOTAL_OFRS; int P[LIMIT+1],i,j; typedef struct { int start_time, stop_time; ULL compensation; } offer; offer offers[LIMIT+1]; ULL solve() { result[0]=offers[0].compensation; ULL result_previous; rep(1,TOTAL_OFRS) { if(P[i]==-1)result_previous=0; else result_previous=result[P[i]];/*Simply include the compensation of the non intersecting segment .P[i] is ind[i] in the editorial explanation*/ result[i] = LARGE(offers[i].compensation + result_previous, result[i - 1]); } return result[TOTAL_OFRS-1]; } int compare(const void * a, const void * b) //comparator used to sort the orders by their respective stop_time { return ((offer*)a)->stop_time > ((offer*)b)->stop_time; } int main() { int runs,flag; scanf("%d",&runs); while(runs--) { scanf("%llu",&TOTAL_OFRS); rep(0,TOTAL_OFRS) { scanf("%d%d%llu",&offers[i].start_time,&offers[i].stop_time,&offers[i].compensation); } qsort(offers, TOTAL_OFRS, sizeof(offer), compare); rep(1,TOTAL_OFRS)/*used to calculate value of P[i]. P[i] is mainly useful to know the nonintersecting segment with the current one*/ { P[i]=0; flag=0; repi(i-1,0) { if(offers[j].stop_time <= offers[i].start_time) { /*used to know the which offer is ended on or before the starting of the current offer start_time*/ P[i]=j; flag=1; break; } } if(flag==0)P[i]=-1;/*nothing but we can't use any other offer to calculate the current compensation since as there are no other events ended on or before the current event's start_time*/ } ULL final_result=solve(); printf("%llu\n",final_result); } return 0; }