Buying Paintings
InterviewTime limit14sMemory limit1024 MB
The paintings sit in a line; walking between adjacent ones costs 1 second and buying painting i costs t_i. Choose k paintings to buy, starting and ending anywhere, so the total buying plus walking time is smallest.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
Mona has just moved and is starting to decorate. She has decided she needs exactly paintings, and she has gone to the art market to shop. Mona is very rich and does not care at all how much the paintings cost; she only wants to finish as quickly as possible.
The market sells paintings along a long street. Buying painting takes seconds. Walking from one painting to the next takes 1 second. Mona takes the bus there and back, so she can choose which painting she starts at and which she ends at. What is the shortest time in which Mona can buy paintings?
Input
The first line contains two integers: (), the number of paintings at the market, and (), the number of paintings Mona needs to buy.
The second line contains integers: , the number of seconds it takes to buy each painting.
Output
Print one integer, the smallest number of seconds it can take Mona to buy paintings.