This page is still under construction.

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

Landscaping

Interview

Time limit1sMemory limit128 MB

Summary
Each flowerbed has a current and target dirt amount, and dirt can be bought, removed, or moved between beds at a per-unit distance cost; find the cheapest way to hit every target.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Implementation, Math
Solved
No attempts yet

Problem

A gardener is landscaping a garden and must move a large amount of dirt in the process.

The garden is a row of NN flowerbeds (1≤N≤1001 \le N \le 100). Flowerbed ii currently holds AiA_i units of dirt, and the gardener wants it to hold BiB_i units instead. Every AiA_i and BiB_i is an integer between 00 and 1010.

Three operations are available:

  • Buy one unit of dirt and place it in any flowerbed, at a cost of XX.
  • Remove one unit of dirt from any flowerbed and ship it away, at a cost of YY.
  • Move one unit of dirt from flowerbed ii to flowerbed jj, at a cost of Z×∣i−j∣Z \times |i - j|.

Compute the minimum total cost to make every flowerbed ii hold exactly BiB_i units of dirt.

Input

  • Line 1: four space-separated integers NN, XX, YY, and ZZ (0≤X,Y,Z≤10000 \le X, Y, Z \le 1000).
  • Lines 22 to N+1N+1: line i+1i+1 contains two space-separated integers AiA_i and BiB_i.

Output

  • A single integer: the minimum total cost to finish the landscaping.

Explanation

In the first example there are 4 flowerbeds holding 1, 2, 3, and 4 units of dirt, with targets of 4, 3, 2, and 0 units. Buying, removing, and moving one unit cost 100, 200, and 1 respectively.

One unit of dirt must be removed (from flowerbed 4) at a cost of 200. The remaining dirt is rearranged by moving 3 units from flowerbed 4 to flowerbed 1 and 1 unit from flowerbed 3 to flowerbed 2, for a moving cost of 10. The total is 210.

Examples3

  1. Example 1

    Input
    4 100 200 1
    1 4
    2 3
    3 2
    4 0
    
    Expected output
    210
    
  2. Example 2

    Input
    2 100 100 1
    10 0
    0 10
    
    Expected output
    10
    
  3. Example 3

    Input
    1 5 7 3
    2 5
    
    Expected output
    15