#141Medium
Ferris wheel
Time: 1 s · Memory: 256 MB
- Sorting
Statement
children want to ride a Ferris wheel; the weight of child is . Each gondola takes one or two children, and the total weight in a gondola must not exceed . What is the minimum number of gondolas needed for all children?
Input format
The first line contains and (, ). The second line contains integers ().
Output format
A single number — the minimum number of gondolas.
Examples
Input
4 10 7 2 3 9
Output
3
Note
Sort the weights and keep two pointers: the lightest () and the heaviest () child. The heaviest child takes some gondola anyway; if the lightest one fits with them, seat them together (moving too), otherwise the heavy child rides alone. decreases at every step, so this is .
reaches , which does not fit into a 32-bit int.
Source: CSES, problem 1090.
