Under construction

Still being built. Things may change

Flying Squirrel

Time limit3sMemory limit1024 MB

Summary
Find the minimum climbing cost for a squirrel to cross all pillars from height L on the first to height R on the last, never touching the ground.
Level

Medium7 of 10

Topics
Dynamic programming, Segment tree, Stack
Solved
No attempts yet

Problem

There are NN pillars in a row on a two-dimensional plane. The pillars are numbered from 1 to NN from left to right.

The base of the ii-th pillar (1≤i≤N1 \le i \le N) is at the point (Di,0)(D_i, 0), and its height is HiH_i. So the pillar is the line segment connecting (Di,0)(D_i, 0) and (Di,Hi)(D_i, H_i). Also, D1=0D_1 = 0.

At the start, the flying squirrel is at height LL on the leftmost pillar, that is, at the point (0,L)(0, L). The squirrel wants to pass through all pillars in order from left to right and reach height RR on the rightmost pillar, that is, the point (DN,R)(D_N, R).

When the squirrel flies from one pillar to the next, if it moves dd (d≥0d \ge 0) to the right, its height decreases by dd. It must not touch the ground before it reaches the next pillar. Arriving at height 0 on the next pillar is allowed.

On a pillar, the squirrel can climb up or move down. It cannot climb above the height of the pillar. Climbing up by hh (h≥0h \ge 0) on the ii-th pillar costs Wi×hW_i \times h. Moving down costs nothing.

Figure 1 shows one possible route of the squirrel.

Figure 1

The route on the left of Figure 2 is not allowed, because the squirrel touches the ground along the way. The route on the right of Figure 2 is also not allowed, because it skips some of the pillars.

Figure 2

Compute the minimum total cost for the squirrel to reach the target position.

Constraints

  • 2≤N≤5000002 \le N \le 500000
  • 0=D1<D2<⋯<DN≤1090 = D_1 < D_2 < \cdots < D_N \le 10^9
  • 1≤Hi≤1091 \le H_i \le 10^9 (1≤i≤N)(1 \le i \le N)
  • 0≤Wi≤1090 \le W_i \le 10^9 (1≤i≤N)(1 \le i \le N)
  • 0≤L≤H10 \le L \le H_1
  • 0≤R≤HN0 \le R \le H_N

Examples1

  1. Example 1

    Input
    2 0 0
    0 1 1
    1 1 0
    
    Expected output
    1