Under construction

Still being built. Things may change

Fascination Street

Time limit1sMemory limit1024 MB

Summary
Given block costs and at most K swaps of costs, find the cheapest set of streetlights that lights every block or touches one.
Level

Medium7 of 10

Topics
Dynamic programming, Array
Solved
No attempts yet

Problem

A street named Fascination Street is divided into NN blocks of equal length. For each block ii (1≤i≤N1 \leq i \leq N), block i−1i-1 is on its left if i>1i > 1, and block i+1i+1 is on its right if i<Ni < N.

Despite its name, the street is dark and eerie at night. To fix this, Robert decided to install streetlights on some of the blocks. Installing a streetlight on block ii costs WiW_i, and the total cost is the sum of the installation costs. After installation, every block must either have a streetlight, or have a streetlight on its left or right neighbor.

Robert can reduce the cost with swaps. Before installing, he picks two distinct blocks ii and jj and swaps their positions, which swaps the values of WiW_i and WjW_j. A swap costs nothing, but Robert can perform at most KK swaps.

Given the array WW and the maximum number of swaps KK, find the minimum cost to light the whole street.

Input

The first line contains two integers NN and KK, separated by a space. NN is the number of blocks, and KK is the maximum number of swaps Robert can perform. (1≤N≤2500001 \leq N \leq 250000, 0≤K≤90 \leq K \leq 9)

The second line contains NN integers W1,W2,⋯ ,WNW_1, W_2, \cdots, W_N, separated by spaces, where WiW_i is the cost of installing a streetlight on block ii. (0≤Wi≤1090 \leq W_i \leq 10^9)

Output

Print a single integer: the minimum cost to light the whole street.

Examples4

  1. Example 1

    Input
    5 0
    1 3 10 3 1
    
    Expected output
    4
    
  2. Example 2

    Input
    5 1
    1 3 10 3 1
    
    Expected output
    2
    
  3. Example 3

    Input
    12 0
    317 448 258 208 284 248 315 367 562 500 426 390
    
    Expected output
    1525
    
  4. Example 4

    Input
    12 2
    317 448 258 208 284 248 315 367 562 500 426 390
    
    Expected output
    1107