This page is still under construction.

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

Driving Lanes

Time limit2sMemory limit512 MB

Summary
Given n straightaways separated by n-1 curves whose cost grows by c per lane, find the minimum total distance from lane 1 back to lane 1, where lane changes cost k+r and must fit within a straightaway.
Level

Hard8 of 10

Topics
Dynamic programming, Implementation, Math
Solved
No attempts yet

Problem

While driving around a curve on the highway, Sam realizes that using the inside lane means traveling a shorter distance. Sam wonders what the minimum distance needed to reach the destination is.

A multilane highway consists of a sequence of straightaways connected by curves. When going around a curve, the distance traveled depends on which lane you are in. Each curve has a curvature cc and a stretch ss. Specifically, if Sam is in lane ii, they travel s+c⋅is + c \cdot i meters while going around this curve.

Whenever Sam is on a straightaway, they may change from one lane into an adjacent lane. When changing to an adjacent lane, Sam moves forward kk meters, but travels a total of k+rk+r meters. Each lane change must be completed before the car reaches the end of the current straightaway. Sam may change lanes multiple times on the same straightaway. For safety reasons, changing lanes is not possible on curves.

Sam starts in lane 11 and wishes to end in lane 11. What is the minimum distance they must travel?

Input

The first line of input contains two integers nn (1≤n≤2501 \leq n \leq 250), which is the number of straightaways, and mm (1≤m≤2501 \leq m \leq 250), which is the number of lanes on the highway. The lanes are numbered 1,2,…,m1, 2, \dots, m.

The second line of input contains two integers kk (1≤k≤1061 \leq k \leq 10^6) and rr (1≤r≤1061 \leq r \leq 10^6), which are the lane changing parameters.

The next nn lines describe the straightaways in order. Each of these lines contains a single integer ℓ\ell (1≤ℓ≤1061 \leq \ell \leq 10^6), which is the length of this straightaway.

The next n−1n-1 lines describe the curves in order. Each of these lines contains two integers ss (1≤s≤1061 \leq s \leq 10^6), which is the stretch of this curve, and cc (−106≤c≤106-10^6 \leq c \leq 10^6), which is the curvature of this curve. It is guaranteed that s+c⋅m>0s + c \cdot m > 0.

The iith curve connects the iith and (i+1)(i+1)th straightaway.

Output

Display the minimum distance Sam must travel.

Examples2

  1. Example 1

    Input
    4 3
    5 2
    10
    10
    10
    10
    4 -1
    4 -1
    4 1
    
    Expected output
    51
    
  2. Example 2

    Input
    4 3
    5 2
    10
    10
    10
    10
    10 -3
    10 -3
    10 1
    
    Expected output
    61