Contest Score
InterviewTime limit2sMemory limit512 MB
Simulate reading problems in order but solving the shortest available one first, keeping at most k in memory, and report the total submission time.
- Level
Medium4 of 10
- Topics
- Simulation, Heap, Greedy
- Solved
- No attempts yet
Problem
You are taking part in a programming contest with n problems. Reading a problem tells you exactly how long it will take you to solve it, and reading costs no time. You can keep the details of at most k problems in your head at once.
Let be the time it takes to solve the ith problem. Your strategy is this:
- Read the first k problems.
- Among the problems you have read and not yet solved, pick one with the shortest solving time. If several are tied for shortest, pick any of them.
- Solve that problem, then read the next unread problem if one is left.
- If unsolved problems remain, go back to step 2.
The submission time of a problem is the time elapsed from the start of the contest until you finish solving it. The penalty time is the sum of the submission times of all problems. Given the order of the problems, compute the penalty time of this strategy.
The answer does not depend on which tied problem you pick in step 2, so it is unique.
Input
The first line contains two integers n and k separated by a space ().
The ith of the next n lines contains one integer ().
Output
Print the penalty time as a single integer on one line.