This page is still under construction.

Parts of this page are still being built. What you see may change.

Two Yachts

Time limit1sMemory limit256 MB

Summary
Pick priced time intervals so no day is covered more than twice and the total price is maximal.
Level

Medium7 of 10

Topics
Dynamic programming, Intervals, Sorting
Solved
No attempts yet

Problem

A marine resort put the use of two luxury yachts up for bidding for the coming season. Everyone who takes part in the bidding submits a sealed proposal that gives a period of use and a bidding price. After the bidding closes, the manager of the resort picks proposals so that the periods of use never overlap on a single yacht, and so that the profit is as large as possible. A participant whose proposal is picked uses one yacht for the submitted period. The season consists of mm days numbered from 1 to mm.

Because there are two yachts, no day may be covered by three or more of the picked periods.

Suppose there are five proposals.

Nobeginning dayending daybidding price
1101840,000
211250,000
32760,000
491630,000
552080,000

The manager cannot take all five, because some days are covered by three or more periods. Picking proposals 2 and 5 gives a profit of 130,000. The largest profit comes from proposals 1, 3, and 5, and it is 180,000.

Given nn proposals, write a program that reports the largest profit the resort can obtain.

Input

Your program reads from standard input. The first line holds the number of test cases TT.

Each test case begins with the number of proposals nn (1≤n≤10 0001 \le n \le 10\,000). Each of the next nn lines holds three integers ss, tt, and pp: the beginning day of the period of use, the ending day of the period of use, and the bidding price (1≤s≤t≤10 000 0001 \le s \le t \le 10\,000\,000, 1≤p≤100 0001 \le p \le 100\,000).

You may assume that the number of proposals overlapping on the same day is at most 100.

Output

Your program writes to standard output. Print exactly one line for each test case, holding an integer, the largest profit the resort can obtain.

Examples2

  1. Example 1

    Input
    2
    5
    10 18 40000
    1 12 50000
    2 7 60000
    9 16 30000
    5 20 80000
    7
    1 3 100
    3 5 100
    5 7 100
    7 9 100
    1 4 100
    5 5 100
    6 9 100
    
    Expected output
    180000
    500
    
  2. Example 2

    Input
    3
    1
    1 1 1
    3
    5 10 40000
    5 10 70000
    5 10 60000
    2
    1 5 100000
    6 10 100000
    
    Expected output
    1
    130000
    200000