Harmonious Magic Basketball Game
Time limit1sMemory limit1024 MB
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 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 , and Luna throws the ball once for each state. State is represented by : if the thrown position satisfies , she earns points.
The hoop states satisfy the following two conditions.
- When is odd, the magic basketball hoop expands from state to state . This means and .
- When is even, the hoop shrinks from state to state . Also, state and state do not overlap. This means , , and .
Luna decides to throw the ball at the same position every time in each round. Specifically, in round she throws the ball only at position 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 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 begins. She throws balls until the end of round 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 through without knowing about Lina's intervention. Only before round 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, and . Find the absolute value of the score obtained as the result of the game.
Input
The first line contains two integers and .
For each from to , the -th line contains the integers , , and that represent state , separated by spaces.
Output
Print the absolute value of the score obtained as the result of the game.
Constraints
- satisfy the conditions in the statement.