Sterilisation
The Central Sterile Services department processes surgical instruments in the order they arrive from the wards. Instruments cannot be reordered, but consecutive instruments may be grouped together. There are instruments in the queue, and instrument
has calibration size
.
The steriliser processes the instruments in contiguous batches. Each batch incurs a fixed setup cost . It also incurs a calibration cost: if the largest calibration size in a batch is
, an instrument of size
contributes
to the calibration cost.
The department must partition the entire queue into one or more contiguous batches. Every instrument must belong to exactly one batch. Find the minimum possible sum of the setup and calibration costs of all batches.
Input
The first line contains two integers and
(
,
), the number of instruments and the fixed setup cost per batch.
The second line contains integers
(
), the calibration sizes of the instruments in queue order.
Output
Output a single integer: the minimum possible total cost to process all instruments.
Example
Input 1
5 3
4 1 5 9 2
Output 1
14
One optimal partition is ,
, and
. Its three batch costs are
,
, and
, for a total cost of
.
Input 2
6 4
10 12 11 50 52 51
Output 2
14
One optimal partition is and
. Each batch has a setup cost of
and a calibration cost of
, giving a total cost of
.
Input 3
8 6
7 3 9 9 2 8 1 5
Output 3
32
Comments