Under construction

Still being built. Things may change

Gas Station

Time limit1sMemory limit1024 MB

Summary
Find the minimum fuel cost to drive from the first gas station to position X, given each station's price, fuel stock, and the tank capacity F.
Level

Medium6 of 10

Topics
Greedy, Stack, Array
Solved
No attempts yet

Problem

Eunha worked hard and finally bought a new car. For a test drive, she plans to drive along a straight road to the destination. The destination is quite far, so she wants to refuel at gas stations along the way. During the test drive, the following conditions must be met.

  • The ii-th gas station is xix_i kilometers to the right of the origin of the road. Fuel bought there costs pip_i per liter, and the station has a total of aia_i liters.
  • At a gas station, she can buy any amount of fuel up to aia_i liters.
  • After refueling at any gas station, the fuel in the car must not exceed the fuel capacity FF liters.
  • The car can travel 1 kilometer on 1 liter of fuel.
  • The new car has no fuel before the test drive. The starting point of the car is the first gas station.
  • The destination is XX kilometers to the right of the origin of the road.

Eunha does not want to waste her hard-earned money on fuel, so she wants to reach the destination at minimum cost. You must help Eunha.

Input

The first line contains the number of gas stations NN, the destination XX, and the fuel capacity FF, separated by spaces. (1≤N≤500 0001 \leq N \leq 500\,000; 1≤X,F≤1091 \leq X, F \leq 10^9)

From the second line, NN lines describe the gas stations. For every 1≤i≤N1 \leq i \leq N, line (i+1)(i+1) contains xix_i, pip_i, and aia_i, separated by spaces. (0≤xi<X0 \leq x_i < X; 1≤pi≤1091 \leq p_i \leq 10^9; 1≤ai≤F1 \leq a_i \leq F) No two gas stations are at the same position, and the positions xix_i are given in ascending order. That is, xi−1<xix_{i-1} < x_i for all 2≤i≤N2 \leq i \leq N.

Output

If the destination cannot be reached by any route during the test drive, print -1 on the first line. Otherwise, print the minimum cost to complete the test drive on the first line.

Examples3

  1. Example 1

    Input
    3 100 60
    0 3 50
    20 4 50
    40 8 40
    
    Expected output
    430
    
  2. Example 2

    Input
    5 210 100
    10 10 40
    30 5 60
    100 4 100
    140 7 30
    190 15 30
    
    Expected output
    1070
    
  3. Example 3

    Input
    5 210 100
    10 10 40
    30 5 60
    100 4 10
    140 7 30
    190 15 30
    
    Expected output
    -1