Under construction

Still being built. Things may change

Kitesurfing

Interview

Time limit4sMemory limit512 MB

Summary
Find the minimum time for Nora to reach the finish by surfing open water or jumping up to d metres over islands, each jump costing t seconds.
Level

Medium5 of 10

Topics
Graph, Shortest path, Geometry
Solved
No attempts yet

Problem

Nora the kitesurfer is taking part in a race across the Frisian islands, a very long and thin archipelago in the north of the Netherlands. The race takes place on the water and follows a straight line from start to finish. Any islands on the route must be jumped over: it is not allowed to surf around them.

The length of the race is ss metres and the archipelago consists of a number of non-intersecting intervals between start and finish line. During the race, Nora can move in two different ways:

  1. Nora can surf between any two points at a speed of 1 metre per second, provided there are no islands between them.
  2. Nora can jump between any two points if they are at most dd metres apart and neither of them is on an island. A jump always takes tt seconds, regardless of distance covered.

While it is not possible to land on or surf across the islands, it is still allowed to visit the end points of any island.

Figure K.1: Illustration of the two sample cases.

Your task is to find the shortest possible time Nora can complete the race in. You may assume that no island is more than dd metres long. In other words, it is always possible to finish the race.

Input

The input consists of:

  • One line with three integers ss, dd and tt (1≤s,d,t≤1091 \le s, d, t \le 10^9), where ss is the length of the race in metres, dd is the maximal jump distance in metres, and tt is the time needed for each jump in seconds.
  • One line with an integer nn (0≤n≤5000 \le n \le 500), the number of islands.
  • nn lines, the iith of which contains two integers ℓi\ell_i and rir_i (0<ℓi<ri<s0 < \ell_i < r_i < s and ri−ℓi≤dr_i - \ell_i \le d), giving the boundaries of the iith island in metres, relative to the starting point.

The islands do not touch and are given from left to right, that is, ri<ℓi+1r_i < \ell_{i+1} for each valid ii.

Output

Output one number, the shortest possible time in seconds needed to complete the race. It can be shown that this number is always an integer.

Examples2

  1. Example 1

    Input
    9 3 4
    2
    2 4
    7 8
    
    Expected output
    11
    
  2. Example 2

    Input
    12 5 3
    3
    1 3
    5 7
    8 11
    
    Expected output
    9