Growing Vegetables is Fun 2
Time limit1sMemory limit512 MB
Choose which plants to pull up so that each remaining plant is the tallest on at least one side of it, maximizing fruit sales minus pulling costs.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Tree, Stack
- Solved
- No attempts yet
문제
JOI, now a professional home gardener, grows IOI grass every year in his own field. He sows seeds in winter, and in spring they sprout and grow to a fixed height. Some IOI grass plants bear beautiful fruit in autumn. A plant that bears fruit is harvested. A plant that does not bear fruit dies in winter.
JOI's field is divided into plots arranged from west to east. Plot holds IOI grass . In spring, IOI grass grows to height and then stops growing. If IOI grass bears fruit, it sells for yen. IOI grass that does not bear fruit has no commercial value.
When JOI checks the field in spring, he may pull up some of the IOI grass to maximize his profit from sales. Pulling up IOI grass (1 ) costs yen. Pulled-up plants die. IOI grass can only be pulled up in spring, not in summer or autumn.
IOI grass needs a lot of sunlight in summer. Suppose a plant in some plot has a taller IOI grass planted in summer both on the plots with smaller numbers and on the plots with larger numbers. Then that plant does not bear fruit in autumn. In other words, if IOI grass (1 ) is not pulled up, it bears fruit in autumn only if no IOI grass taller than IOI grass is in plots during summer, or no IOI grass taller than IOI grass is in plots during summer.
Profit is the total sales price of the fruiting IOI grass minus the total cost of pulling up plants. What is the maximum profit JOI can earn from the IOI grass?
Given the information about the field and the IOI grass planted in it, write a program that finds the maximum profit JOI can earn.
Input
The first line contains an integer , the number of plots in JOI's field.
Each of the next lines contains three integers , , and , separated by spaces, for . This means that IOI grass grows to height in spring, sells for yen if it bears fruit in autumn, and costs yen to pull up.
Output
Print the maximum profit JOI can earn as one integer on a single line.
Constraints
- for
- for
- for