Wednesday, June 8, 2022

Maximise Score

 You are given 

N segments where each segment is of the form [Li,Ri] (1LiRiN).

It is ensured that for any two segments X and Y, one of the following conditions hold true:

  • XY=X,
  • XY=Y, or
  • XY= (empty set)

Note that  denotes the intersection of segments.

In other words, for any two segments, either one is completely inside the other or they are completely disjoint.

The ith segment (1iN) is assigned a value Vi.
A subset S={S1,S2,,Sk} having k segments is considered good if there exists an integer array B of size k such that:

  • All elements of the array B are distinct.
  • Bi lies in the segment Si.

Let the score of a good subset be defined as the sum of values of all segments of the subset. Find the maximum score of a good subset.

Input Format

  • The first line of input contains a single integer T - the number of test cases. The description of T test cases follow.
  • The first line of each test case contains integers N - the number of segments.
  • Each of the next N lines contains three integers Li,Ri, and Vi, describing the ith segment.

Output Format

For each test case, output the maximum score of a good subset.

Constraints

  • 1T105
  • 1N105
  • 1LiRiN
  • 1Vi109
  • Sum of N over all test cases does not exceed 3105.

Sample Input 1 

2
4
2 2 5
2 2 10
3 3 14
3 3 20
4
1 3 4
2 3 5
1 1 10
1 4 13

Sample Output 1 

30
32

Explanation

Test case 1: A possible value of good subset is S=[A2,A4]. The array B=[2,3] such that 2 lies in the segment A2 and 3 lies in the segment A4. The sum of values of all segments of S is 10+20=30.
It can be proven that the score of a good subset does not exceed 30 in this case.

Test case 2: We can take S=[A1,A2,A3,A4]. The array B=[2,3,1,4].

  • For segment A1L=1 and R=3. Here L12R1.
  • For segment A2L=2 and R=3. Here L23R2.
  • For segment A3L=1 and R=1. Here L31R3.
  • For segment A4L=1 and R=4. Here L44R4.

The sum of scores of all segments of S is 4+5+10+13=32. It can be proven that the score of a good subset does not exceed 4+5+10+13=32 in this case.

No comments:

Post a Comment

मुझे दिल में बसाना आसान नहीं....

पता है Recently i realised की मुझे दिल में बसाना आसान नहीं है बात-बात पे चिढ़ जाती हूं मैं दिल लगा लूं एक बार तो फिर ज़िद पे अड़...