RENT - Rent your airplane and make money


"ABEAS Corp." is a very small company that owns a single airplane. The customers of ABEAS Corp are large airline companies which rent the airplane to accommodate occasional overcapacity.

Customers send renting orders that consist of a time interval and a price that the customer is ready to pay for renting the airplane during the given time period. Orders of all the customers are known in advance. Of course, not all orders can be accommodated and some orders have to be declined. Eugene LAWLER, the Chief Scientific Officer of ABEAS Corp would like to maximize the profit of the company.

You are requested to compute an optimal solution.

Small Example

Consider for instance the case where the company has 4 orders:

  • Order 1 (start time 0, duration 5, price 10)
  • Order 2 (start time 3, duration 7, price 8)
  • Order 3 (start time 5, duration 9, price 7)
  • Order 4 (start time 6, duration 9, price 8)

The optimal solution consists in declining Order 2 and 3 and the gain is 10+8 = 18.
Note that the solution made of Order 1 and 3 is feasible (the airplane is rented with no interruption from time 0 to time 14) but non-optimal.

Input

The first line of the input contains a number T ≤ 30 that indicates the number of test cases to follow. The first line of each test case contains the number of orders n (n ≤ 10000). In the following n lines the orders are given. Each order is described by 3 integer values: The start time of the order st (0 ≤ st < 1000000), the duration d of the order (0 < d < 1000000), and the price p (0 < p < 100000) the customer is ready to pay for this order.

Output

You are required to compute an optimal solution. For each test case your program has to write the total price paid by the airlines.

Example

Input:
1
4
0 5 10
3 7 14
5 9 7
6 9 8

Output:
18
Warning: large Input/Output data, be careful with certain languages

hide comments
xxbloodysantaxx: 2015-07-12 19:31:10

How can we solve it using Segment Trees??
**EDIT**
DiD it :)

Last edit: 2015-07-17 16:24:50
xxx_joker: 2015-05-12 10:36:50

activity selection it is!!!!!

Rajat (1307086): 2015-03-29 21:51:08

"Greedy approach does not always give an optimal solution."

NARUTO (y): 2014-11-06 07:38:41

for this test case
1
10
0 5 6
2 3 1
8 5 11
15 2 1
15 5 10
1 4 3
20 1 1
20 2 10
25 2 15
25 2 11
ans should be 52 but due to weak test cases 43 also accepted
ans like 43 accepted because
thsi condition is already assumed that previous end time cannot be equal to current starting time
ex
1
2
0 5 10
5 9 7
in this second one can not start form 5 because first one end at 5 .
but in question it is written that "the airplane is rented with no interruption"

Md Abdul Alim: 2014-10-01 07:46:40

Nice One!!

Vipul Pandey: 2014-02-05 22:28:47

really a nice one!

rohit2100: 2014-01-02 13:47:07

O(n*n*t) is giving TLE!!
what is the complexity of AC solution
EDIT: there is a faster solution

Last edit: 2014-01-05 15:13:30
Dhruva Bhaswar: 2013-05-03 08:44:03

1
10
0 5 6
2 3 1
8 5 11
15 2 1
15 5 10
1 4 3
20 1 1
20 2 10
25 2 15
25 2 11
my accepted solution gives 43 while the answer should be 52.

thefourtheye: 2013-01-12 06:10:18

Does the input has a case with two Orders ending at the same time?


Added by:Adrian Kuegel
Date:2004-07-13
Time limit:3s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ERL JS-RHINO NODEJS PERL6 VB.NET
Resource:ACM Southwestern European Regional Contest, Paris 2003