Given $N$ budget requests and a total budget cap, allocate as much as possible: if all requests fit, grant them fully; otherwise pick an integer ceiling $c$ so that any request above $c$ receives exactly $c$ and the rest receive their full request, maximizing the total allocated without exceeding the cap. Print the maximum such ceiling (or the largest request if all fit).
Input
Line 1: $N$. Line 2: the $N$ requests. Line 3: the total budget.
Output
Print the maximum ceiling value.
Example 1
Input
4 120 110 140 150 485
Output
127
Example 2
Input
5 70 80 30 40 100 450
Output
100
Discussion
Threads about this problem. Spoilers are collapsed; wrap hints in
||double bars||.No discussion yet. Start a thread if something here needs explaining.