#P2677. [USACO07DEC] Bookshelf 2 B
[USACO07DEC] Bookshelf 2 B
Description
Farmer John recently added a huge bookshelf to the cows' library. Although it is very large, it was almost instantly filled with all kinds of books. Now, only a bit of space remains at the top of the shelf. All cows () each have a fixed height () (such tall cows >_<). Let the sum of all cow heights be . The shelf height is , and it is guaranteed that .
To reach the top of the shelf, which is higher than the tallest cow, the cows have to perform acrobatics: one stands on another's back, forming a “cow tower.” The height of the tower is the sum of the heights of the cows in it. To place items on the top of the shelf, the sum of the heights of the cows in the tower must be at least the shelf height.
The taller the tower, the more unstable it becomes, so the cows want to find a plan such that, among all towers with height at least , the height is as small as possible. Your task is to write a program to compute, under this requirement, how much taller than the shelf the tower must be, minimized.
Input Format
Line : Two space-separated integers: and .
Lines to : Line contains a single integer .
Output Format
Line : Output a non-negative integer, the minimal amount by which the tower’s height exceeds the shelf height.
5 16
3
1
3
5
6
1
Hint
We choose cows , , , and to form a tower; their total height is . No arrangement can form a tower of height , so the answer is .
Translated by ChatGPT 5
京公网安备 11011102002149号