This page is still under construction.

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

Group Division

Time limit1.5sMemory limit1024 MB

Level

Not classified yet

Solved
No attempts yet

Problem

Divide NN people into groups. Each person belongs to exactly one group, and each group has exactly one leader. Person ii has three integers a_ia\_i, b_ib\_i, and c_ic\_i that describe their leadership ability. Person ii can lead a group of xx people if x≤c_ix \le c\_i. If c_i=1c\_i=1, person ii must be alone in their group to lead. The strength of such a group is the integer a_i⋅x+b_ia\_i\cdot x + b\_i. Divide the people into groups so that the sum of the group strengths is maximized.

Input

The first line contains an integer NN (1≤N≤40001 \le N \le 4000), the number of people. Each of the next NN lines contains three integers a_ia\_i, b_ib\_i, and c_ic\_i (−109≤a_i,b_i≤109-10^9 \le a\_i, b\_i \le 10^9, 1≤c_i≤N1 \le c\_i \le N).

Output

Print one integer: the maximum possible sum of group strengths.

Hints

In the first sample, the maximum strength is achieved, for example, by splitting the people into three groups: one with persons 1 and 4 (leader 1), one with persons 3 and 5 (leader 3), and one with person 2. This gives (10⋅2+7)+(−1⋅1+20)+(5⋅2+10)=66(10 \cdot 2 + 7) + (-1 \cdot 1 + 20) + (5 \cdot 2 + 10) = 66 strength. This test case could be part of test group 3.

In the second sample, the maximum strength is achieved, for example, by splitting the people into two groups: one with persons 1, 2, and 3 (leader 3), and one with persons 4 and 5 (leader 4). This gives (10⋅2+−20)+(11⋅3+−30)=3(10 \cdot 2 + -20) + (11 \cdot 3 + -30) = 3 strength. This test case could be part of test group 4.

Examples3

  1. Example 1

    Input
    5
    10 7 2
    -1 20 4
    5 10 3
    2 2 2
    2 2 2
    
    Expected output
    66
    
  2. Example 2

    Input
    5
    6 -40 4
    7 -40 4
    10 -20 2
    11 -30 3
    12 -10 1
    
    Expected output
    3
    
  3. Example 3

    Input
    4
    1000000000 1000000000 2
    -1000000000 10 2
    900000000 -1000000000 2
    -20 -25 1
    
    Expected output
    3800000000