#C7037. Minimizing the Sum of a Subarray with Limited Swaps

    ID: 50864 Type: Default 1000ms 256MiB

Minimizing the Sum of a Subarray with Limited Swaps

Minimizing the Sum of a Subarray with Limited Swaps

You are given an array \(A\) of \(N\) integers and two integers \(K\) and \(D\). You are allowed to perform at most \(D\) swap operations on any two elements of the array. After performing these swaps, your task is to select a contiguous subarray of length \(K\) such that its sum is minimized.

It can be proven that by performing optimal swaps, the array can be rearranged in non-decreasing order. In this case, the optimal subarray is simply the first \(K\) elements of the sorted array, and the answer is given by:

\(S = \sum_{i=1}^{K} B_i\)

where \(B_1, B_2, ..., B_N\) is the array \(A\) sorted in non-decreasing order.

Note: Even if no swaps are allowed (i.e. \(D = 0\)), for the purpose of this problem, you should assume that the optimal outcome equals the sum of the \(K\) smallest elements of \(A\).

inputFormat

The input is given from stdin as follows:

  • The first line contains three space-separated integers \(N\), \(K\), and \(D\).
  • The second line contains \(N\) space-separated integers representing the array \(A\).

You can assume that \(1 \le K \le N\) and that the input values fit into the appropriate data types.

outputFormat

Print a single integer—the minimum possible sum of a contiguous subarray of length \(K\) after performing at most \(D\) swaps (which, as shown, is equivalent to the sum of the \(K\) smallest elements of \(A\)). The output should be sent to stdout.

## sample
5 3 1
1 2 5 2 1
4