This page is still under construction.

Parts of this page are still being built. What you see may change.

Fuel Economy

Interview

Time limit1sMemory limit128 MB

Summary
Find the cheapest way to buy fuel along a route with a tank of capacity G, visiting stations with given prices, or report that the trip is impossible.
Level

Medium7 of 10

Topics
Greedy, Stack, Array, Sorting
Solved
No attempts yet

Problem

A driver is setting out on a long road trip. The truck has a fuel tank that can hold at most GG units of fuel (1≤G≤1,000,0001 \le G \le 1{,}000{,}000). The truck gets poor mileage: it burns exactly one unit of fuel for every unit of distance traveled, and the whole trip is DD units of distance long (1≤D≤1,000,000,0001 \le D \le 1{,}000{,}000{,}000).

Because the tank may need to be refilled several times along the way, the driver lists all NN fuel stations on the route (1≤N≤50,0001 \le N \le 50{,}000). Station ii sits at distance XiX_i from the start (0≤Xi≤D0 \le X_i \le D) and sells fuel at a price of YiY_i per unit (1≤Yi≤1,000,0001 \le Y_i \le 1{,}000{,}000).

The truck begins the trip with exactly BB units of fuel already in the tank (0≤B≤D0 \le B \le D). Determine the minimum total amount of money that must be spent on fuel to reach the destination at distance DD. If reaching the destination is impossible, output −1-1 instead.

Note: the answer may not fit in a signed 3232-bit integer.

Input

  • Line 11: four space-separated integers NN, GG, BB, and DD.
  • Lines 2…N+12 \dots N+1: line i+1i+1 contains two integers XiX_i and YiY_i, the position and per-unit price of station ii.

Output

  • One line: the minimum cost to reach the destination, or −1-1 if the destination cannot be reached.

Hint

In the first case the route runs from position 00 to D=17D = 17. The truck starts with 33 units of fuel in a tank of capacity 1010, and there are 44 stations.

One optimal plan: drive 22 units to the station at position 22 and buy 22 units there (cost 40×240 \times 2) to reach the station at position 55; fill the tank completely there (cost 7×107 \times 10); at position 1010 buy 22 more units (cost 12×212 \times 2). The total cost is 174174.

A greedy strategy works: at each station, if a cheaper station lies within one full tank's range, buy only enough fuel to reach it; otherwise fill the tank completely and continue to the cheapest reachable station.

Examples1

  1. Example 1

    Input
    4 10 3 17
    2 40
    9 15
    5 7
    10 12
    
    Expected output
    174