Under construction

Still being built. Things may change

Harmonious Magic Basketball Game

Time limit1sMemory limit1024 MB

Summary
Given N alternating hoop states and K rounds, find the final absolute score when Luna optimizes non-consecutive blessings and Lina secretly chooses a cursed interval to minimize it.
Level

Hard9 of 10

Topics
Dynamic programming, Segment tree, Intervals, Game theory
Solved
No attempts yet

Problem

Magician Luna visited a magic amusement park after a long time and found her favorite game, the magic basketball game.

The magic basketball game has KK rounds. In each round the hoop keeps changing shape. Specifically, in every round the hoop starts at state 1 and changes in order through state NN, and Luna throws the ball once for each state. State jj is represented by (Lj,Rj,Sj)(L_j, R_j, S_j): if the thrown position xx satisfies Lj≤x≤RjL_j \leq x \leq R_j, she earns SjS_j points.

The hoop states satisfy the following two conditions.

  • When jj is odd, the magic basketball hoop expands from state jj to state j+1j+1. This means Lj+1≤LjL_{j+1} \leq L_j and Rj≤Rj+1R_j \leq R_{j+1}.
  • When jj is even, the hoop shrinks from state jj to state j+1j+1. Also, state j+1j+1 and state j−1j-1 do not overlap. This means Lj≤Lj+1L_j \leq L_{j+1}, Rj+1≤RjR_{j+1} \leq R_j, and [Lj−1,Rj−1]∩[Lj+1,Rj+1]=∅[L_{j-1},R_{j-1}] \cap [L_{j+1},R_{j+1}] = \varnothing.

Luna decides to throw the ball at the same position every time in each round. Specifically, in round ii she throws the ball only at position ii every time. She never misses the position by mistake.

Just before throwing a ball, Luna can use her magic to put a blessing on it. If a blessed ball enters the hoop, she earns double points. However, the magic has a cooldown, so she cannot bless two consecutive attempts in one round.

Luna wants to maximize the total score earned over KK rounds. But she does not know at all that her other self, the dark magician Lina, wants to interfere.

Lina waits quietly behind her other self, then suddenly appears just before round aa begins. She throws balls until the end of round bb and returns behind her other self. Lina throws every ball with a curse instead of a blessing, so a successful shot subtracts the points she would have earned. Luna plays rounds 11 through a−1a-1 without knowing about Lina's intervention. Only before round b+1b+1 begins does she notice that Lina was there. Now that things have turned out this way, she continues the game according to her philosophy, aiming to maximize the absolute value of the score.

Lina knows this as well. She will do her best to minimize the absolute value of the score, and she can choose in advance, without Luna knowing, both the first and last rounds of her appearance, aa and bb. Find the absolute value of the score obtained as the result of the game.

Input

The first line contains two integers NN and KK.

For each ii from 11 to NN, the (i+1)(i+1)-th line contains the integers LiL_i, RiR_i, and SiS_i that represent state ii, separated by spaces.

Output

Print the absolute value of the score obtained as the result of the game.

Constraints

  • 1≤N,K≤1061 \leq N,K \leq 10^6
  • 1≤Lj≤Rj≤1061 \leq L_j \leq R_j \leq 10^6
  • 1≤Sj≤1061 \leq S_j \leq 10^6
  • Lj,Rj,SjL_j, R_j, S_j satisfy the conditions in the statement.

Examples1

  1. Example 1

    Input
    7 10
    1 3 5
    1 7 1
    4 5 2
    2 9 3
    8 8 12
    5 8 2
    5 6 3
    
    Expected output
    2