#P9468. Minimizing Swaps to Achieve Candy Threshold
Minimizing Swaps to Achieve Candy Threshold
Minimizing Swaps to Achieve Candy Threshold
In the ancient city of Ika, a legendary palace is said to hold unimaginable wealth. In one of its corridors, there are (N) boxes containing candies from around the world. Travelers who pass by can take any number of candies if they pay gold equal to the weight of the candies they take.
The boxes are numbered from (0) to (N-1) (from left to right). For each (i), the (i)th box contains (a_i) units of candy (where (a_i \ge 0)).
As the guardian of the palace, you have been tasked with rearranging the boxes so that the boxes with more candies are positioned closer to the entrance. You are given the array (a_0,a_1,\dots,a_{N-1}) and two integers (F) and (T). In one operation, you are allowed to swap any two adjacent elements in the array. What is the minimum number of operations required to ensure that the sum of the first (F) elements is at least (T)?
Note: If it is impossible to achieve the desired condition, output (-1).
inputFormat
The input consists of two lines:
The first line contains three integers (N), (F), and (T).
The second line contains (N) non-negative integers (a_0, a_1, \dots, a_{N-1}), representing the number of candy units in each box.
outputFormat
Output a single integer: the minimum number of adjacent swaps required so that the sum of the first (F) boxes is at least (T). If it is impossible, output (-1).
sample
5 3 15
2 1 4 7 6
2