AmazonOA #216
Get Min Cost
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
At each step you may buy the leftmost or the rightmost remaining book at its original price, or buy the two different books at both ends together for pairCost. Joint purchases can be made at most k times. Find the minimum cost to buy all the books.
Input
The first line contains n pairCost k; the second line contains the prices. 1≤n≤100000, 1≤price,pairCost≤10^9, 1≤k≤n.
Output
Output a single integer, the answer.
Example 1
Input:
3 2 1
1 2 3
Output:
3
Example 2
Input:
4 1 1
100 1 1 100
Output:
3
Example 3
Input:
3 10 3
1 1 1
Output:
3
Example notes
The answers are 3, 3 and 3. In the second example the two 100s at the ends are bought together; when the discount does not pay off, you may skip it.
Editorial
Loading solution…