Klocki
Time limit1sMemory limit512 MB
Choose at most k of the n blocks so their total mass is as large as possible without exceeding s.
- Level
Medium6 of 10
- Topics
- Divide and conquer, Sorting, Binary search
- Solved
- No attempts yet
Problem
Bajtek has a great many blocks, and he loves playing with them. Unfortunately he has only one box for them, and it is so small that not all of the blocks fit inside.
Bajtek is a very tidy boy and does not like leaving a mess in his room, so after playing he always packs the blocks into the box and puts the box on a shelf.
All blocks are the same size, so no matter which ones he picks he can fit at most blocks into the box. As far as possible he would like to leave only the light blocks on the floor, so he always tries to pack the heavier ones. Sometimes, though, the box turns out to be too heavy for him to lift onto the shelf, because Bajtek is only a little boy. He therefore tries to pack the blocks so that their total mass is as large as possible while still being light enough for him to lift.
Bajtek is tired of repacking the blocks just because he is not strong enough to lift the box. Write a program that tells him how to pack the blocks optimally.
Input
The first line contains three integers , , and (, , ), separated by single spaces. They denote the total number of blocks, the maximum number of blocks that fit in the box, and Bajtek's strength (the maximum mass of a box he can lift), respectively.
The second line contains integers (), separated by single spaces, denoting the masses of the individual blocks.
The mass of the box itself is ignored (you may assume it is ).
Output
Print a single integer : the maximum mass of a box loaded with blocks that Bajtek can lift.
Hint
An empty box has mass , which Bajtek can always lift, so the answer is never less than . For example, when , at most blocks may be packed, and the block masses are , the best choice is to pack the blocks of mass and for a total of . The block of mass already exceeds on its own, and combinations such as or also exceed .