#K57642. Longest Subsequence with Sum Constraint

    ID: 30466 Type: Default 1000ms 256MiB

Longest Subsequence with Sum Constraint

Longest Subsequence with Sum Constraint

You are given an array of positive integers and an integer \(k\). Your task is to find the length of the longest subsequence (i.e., a subset of the array elements) such that the sum of its elements is less than or equal to \(k\).

Note: The order of elements in the subsequence does not matter. A greedy strategy by selecting the smallest elements first works in this case.

Constraints:

  • \(1 \leq n \leq 10^5\), where \(n\) is the number of elements in the array.
  • \(1 \leq a_i \leq 10^9\) for each element \(a_i\) in the array.
  • \(0 \leq k \leq 10^{18}\).

The answer is guaranteed to be the maximum number of elements that can be chosen while keeping their sum \(\le k\).

inputFormat

The first line contains two integers \(n\) and \(k\) separated by a space, where \(n\) is the number of elements in the array and \(k\) is the maximum allowed sum.

The second line contains \(n\) space-separated integers representing the array elements.

outputFormat

Output a single integer: the maximum length of a subsequence such that the sum of its elements is less than or equal to \(k\).

## sample
5 10
1 2 3 4 5
4