Fascination Street
Time limit1sMemory limit1024 MB
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 blocks of equal length. For each block (), block is on its left if , and block is on its right if .
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 costs , 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 and and swaps their positions, which swaps the values of and . A swap costs nothing, but Robert can perform at most swaps.
Given the array and the maximum number of swaps , find the minimum cost to light the whole street.
Input
The first line contains two integers and , separated by a space. is the number of blocks, and is the maximum number of swaps Robert can perform. (, )
The second line contains integers , separated by spaces, where is the cost of installing a streetlight on block . ()
Output
Print a single integer: the minimum cost to light the whole street.