Complete Histogram

0

0 votes
Segment tree, Dynamic programming, Algorithms, Medium-Hard, Dynamic programming
Problem

Histograms are generally fun to play with. But not all histograms are complete. Making a normal histogram complete, is not such an easy task. We will attempt to do it here.

Let us first define a complete histogram. A histogram is complete iff it satisfies the following conditions -

  1. There have to be N bars in the histogram.
  2. The height of any bar in the histogram should be either 0 or M.
  3. For any pair of i,j, if ((i==1 or Heighti1!=Heighti) and (j==N or Heightj+1!=Heightj) and (Heightk==Heighti, for all ikj)) holds, then aji+1b should hold.

 

You are given a histogram, which may or may not be complete. It has to be converted into a complete histogram with minimum cost incurred. You are allowed to increase or decrease height of any bar in it. The cost of increasing any bar by unit height costs x and decreasing any bar by unit height costs y.

Input:
The first line of the input contains 6 integers, N,M,a,b,x,y, as mentioned in the problem statement.
The second line contains N integers, representing an array A of size N, where the element at index i represents Heighti.

Output:
A single integer representing the minimum cost needed to convert the histogram into a complete histogram. Print -1 if it is not possible to make a complete histogram.

Constraints:
1N105
1M105
1x105
1y105
0<abN
0HeightiM

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

We can make 1st, 3rd and 5th bar of height 10 and 2nd, 4th bars of height 0. The total cost is 6+2+1+6+5=20.

Editor Image

?