Editorial for CPC '19 Contest 1 P2 - Luggage
Submitting an official solution before solving the problem yourself is a bannable offence.
For this subtask, you can simply brute force all subsets of the items and pick the largest subset with a height range ~\le K~.
Time Complexity: ~\mathcal O(2^N \cdot N)~
Note that it is always advantageous to pick items with as close of a height as possible. Therefore, if you sort the items the question simply becomes finding the largest subarray with a height range ~\le K~. Brute forcing all subarrays and picking the largest one with a height range ~\le K~ is fast enough for the second subtask. Finding the height range of a subarray from indices ~i~ to ~j~ is simply ~arr[j]-arr[i]~ since the array is sorted.
Time Complexity: ~\mathcal O(N^2)~
To get full points, you have to optimize the brute force from subtask 2 to either binary search or a two-pointer method.
Time Complexity: ~\mathcal O(N \log N)~