Under construction

Still being built. Things may change

Orange Shipping

Time limit1sMemory limit256 MB

Summary
Partition the ordered oranges into consecutive boxes of at most M to minimize the sum of K plus size times the size spread in each box.
Level

Medium6 of 10

Topics
Dynamic programming, Sliding window
Solved
No attempts yet

Problem

Juicy Orange Industry (JOI) is a company that packs oranges into boxes and ships them.

JOI ships the NN oranges it has collected by putting them into boxes. The oranges are first laid out in a row on a conveyor belt in the factory. The oranges on the belt are numbered 11 through NN from the front, and the size of orange ii is AiA_i.

The next step is to put the oranges into boxes, in order from the front. The numbers of the oranges placed in one box must be consecutive.

One box holds at most MM oranges. The cost of filling a box is K+s×(a−b)K + s \times (a - b), where aa is the largest orange size in that box, bb is the smallest size, and ss is the number of oranges in it. KK is the fee for packing a box and is the same for every box.

Given the oranges on the conveyor belt, the maximum number of oranges that one box holds, and the packing fee KK, write a program that finds the minimum total cost of packing all of the oranges.

Input

The first line contains the number of oranges NN (1≤N≤20 0001 \le N \le 20\,000), the maximum number of oranges in one box MM (1≤M≤1 0001 \le M \le 1\,000, M≤NM \le N), and the packing fee KK (0≤K≤1 000 000 0000 \le K \le 1\,000\,000\,000), separated by spaces.

Each of the next NN lines contains one orange size AiA_i (1≤Ai≤1 000 000 0001 \le A_i \le 1\,000\,000\,000), given in order.

Output

Print the minimum total cost of packing all of the oranges on the first line.

Examples4

  1. Example 1

    Input
    6 3 6
    1
    2
    3
    1
    2
    1
    
    Expected output
    21
    
  2. Example 2

    Input
    16 4 12
    3
    10
    13
    10
    19
    9
    12
    16
    11
    2
    19
    9
    13
    2
    13
    19
    
    Expected output
    164
    
  3. Example 3

    Input
    16 6 14
    19
    7
    2
    15
    17
    7
    14
    12
    3
    14
    5
    10
    17
    20
    19
    12
    
    Expected output
    177
    
  4. Example 4

    Input
    10 1 1000000000
    1
    1
    1
    1
    1
    1
    1
    1
    1
    1
    
    Expected output
    10000000000