All problems

Problem 907

Total Cost to Hire K Workers

Medium · 20 pts · Array, Heap, Two Pointers

Problem

Worker i costs COSTS[i]. Run K hiring sessions. In each session, look only at the first C and the last C workers still available (the two groups may overlap when few remain) and hire the cheapest of them, breaking ties by the smallest index; the hired worker is removed. Print the total cost of the K hires. Keep two min-heaps, one for each end, and refill them from two pointers moving inward.

Input

The first line contains N, K and C. The second line contains the N costs.

Output

Print the total hiring cost.

Constraints

1 <= N <= 100000; 1 <= K, C <= N; 1 <= COSTS[i] <= 10^5

Example input

9 3 4
17 12 10 2 7 2 11 20 8

Example output

11

Explanation

For costs 17 12 10 2 7 2 11 20 8 with K = 3 and C = 4: hire the 2 at index 3, then the 2 at index 5, then the 7 at index 4. The total is 11.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Process Tasks Using Servers Next: Maximum Performance of a Team