Under construction

Still being built. Things may change

Growing Vegetables is Fun 2

Time limit1sMemory limit512 MB

Summary
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 NN plots arranged from west to east. Plot ii holds IOI grass ii. In spring, IOI grass ii grows to height HiH_i and then stops growing. If IOI grass ii bears fruit, it sells for PiP_i 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 ii (1 ≤\le ii ≤\le NN) costs CiC_i 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 ii (1 ≤\le ii ≤\le NN) is not pulled up, it bears fruit in autumn only if no IOI grass taller than IOI grass ii is in plots 1,…,i−11, \ldots, i-1 during summer, or no IOI grass taller than IOI grass ii is in plots i+1,…,Ni+1, \ldots, N 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 NN, the number of plots in JOI's field.

Each of the next NN lines contains three integers HiH_i, PiP_i, and CiC_i, separated by spaces, for 1≤i≤N1 \le i \le N. This means that IOI grass ii grows to height HiH_i in spring, sells for PiP_i yen if it bears fruit in autumn, and costs CiC_i yen to pull up.

Output

Print the maximum profit JOI can earn as one integer on a single line.

Constraints

  • 3≤N≤1000003 \le N \le 100000
  • 1≤Hi≤10000000001 \le H_i \le 1000000000 for 1≤i≤N1 \le i \le N
  • 1≤Pi≤10000000001 \le P_i \le 1000000000 for 1≤i≤N1 \le i \le N
  • 1≤Ci≤10000000001 \le C_i \le 1000000000 for 1≤i≤N1 \le i \le N

Examples3

  1. Example 1

    Input
    7
    22 60 30
    46 40 30
    36 100 50
    11 140 120
    38 120 20
    24 90 60
    53 50 20
    
    Expected output
    320
    
  2. Example 2

    Input
    5
    18 150 180
    18 380 250
    18 140 170
    17 180 900
    14 150 520
    
    Expected output
    1000
    
  3. Example 3

    Input
    8
    52 156 59
    15 166 185
    16 122 115
    24 161 154
    44 252 678
    32 225 557
    44 155 254
    59 57 253
    
    Expected output
    854