Under construction

Still being built. Things may change

Jailbreak

Time limit2sMemory limit512 MB

Summary
Partition L cells into at most G consecutive blocks, minimizing the sum over each cell of its escape power times its block length.
Level

Hard8 of 10

Topics
Dynamic programming, Divide and conquer, Prefix sum
Solved
No attempts yet

Problem

A prison has LL cells in a row. The cells are numbered 1 through LL, and cell ii holds one prisoner. The escape power of the prisoner in cell ii is CiC_i, a number that measures how well that prisoner can break out.

The ideal way to watch the prison is to put one guard on every cell, so that each guard watches a single prisoner. Because of the budget, this prison can hire at most GG guards. You want to hire and place the guards so that the escape risk is as small as possible.

Each guard watches cells with consecutive numbers, and every cell is watched by exactly one guard. The escape risk RiR_i of cell ii is the escape power CiC_i multiplied by the number of prisoners watched by the guard in charge of cell ii. The escape risk of the prison is the sum of RiR_i over all cells.

Given LL, GG, and the escape power CiC_i of the prisoner in each cell, write a program that finds the minimum escape risk.

Input

The first line contains the size of the prison LL and the number of guards GG, separated by a space. (1≤L≤80001 \le L \le 8000, 1≤G≤8001 \le G \le 800)

The second line contains C1,C2,…,CLC_1, C_2, \dots, C_L separated by spaces. (1≤Ci≤1091 \le C_i \le 10^9)

Output

Print the minimum escape risk on the first line.

Hint

In the first example the escape risk is smallest when one guard watches cells 1, 2, 3, another guard watches cells 4, 5, and the remaining guard watches cell 6.

The guard in charge of cells 1, 2, 3 watches three prisoners, so the escape risk of each of those cells is 11×3=3311 \times 3 = 33.

The guard in charge of cells 4, 5 watches two prisoners, so cell 4 gives 24×2=4824 \times 2 = 48 and cell 5 gives 26×2=5226 \times 2 = 52.

The guard in charge of cell 6 watches one prisoner, so cell 6 gives 100×1=100100 \times 1 = 100. The total is 33×3+48+52+100=29933 \times 3 + 48 + 52 + 100 = 299.

Examples5

  1. Example 1

    Input
    6 3
    11 11 11 24 26 100
    
    Expected output
    299
    
  2. Example 2

    Input
    1 1
    1000000000
    
    Expected output
    1000000000
    
  3. Example 3

    Input
    5 5
    1 2 3 4 5
    
    Expected output
    15
    
  4. Example 4

    Input
    4 1
    1 2 3 4
    
    Expected output
    40
    
  5. Example 5

    Input
    2 1
    1000000000 1000000000
    
    Expected output
    4000000000