#K43717. Cumulative Sum Rearrangement
Cumulative Sum Rearrangement
Cumulative Sum Rearrangement
You are given an array of N integers and a threshold value X. Your task is to determine whether it is possible to rearrange the elements of the array such that the cumulative sum (the sum of elements taken in order) never exceeds X at any point.
Formally, if a permutation P of the array A is chosen, let Si = AP1 + AP2 + ... + APi for every 1 ≤ i ≤ N. Determine if there exists a permutation such that Si ≤ X for all i.
This problem can be solved using a greedy approach by sorting the array in non-decreasing order, which minimizes the cumulative sums. If the cumulative sum in this order never exceeds X, then the answer is True; otherwise, it is False.
inputFormat
The input is read from standard input (stdin) and is structured as follows:
- The first line contains an integer T, the number of test cases.
- For each test case, there are two lines:
- The first line contains two integers N and X where N is the number of elements in the array and X is the threshold.
- The second line contains N space-separated integers representing the array.
outputFormat
For each test case, output a single line to standard output (stdout) containing either True
if there exists a rearrangement such that the cumulative sum never exceeds X, or False
otherwise.
1
5 10
3 1 4 2 5
False
</p>