Distribute a total study time across several days so each day receives a value within a given range, if possible.
You are given days before an exam. For each day , you know a minimum and maximum amount of time you can study on that day: from to hours inclusive.
Your task is to assign an integer number of study hours to every day so that:
- the total sum of assigned hours is exactly ;
- for every day , the assigned value lies in the interval .
If such an assignment exists, output one valid schedule. Otherwise, report that it is impossible.
A common way to think about the problem is to start with the minimum required time for every day, then distribute the remaining hours without exceeding the daily upper bounds.
Input Format
- The first line contains two integers and .
- The next lines each contain two integers and describing the allowed range for day .
Output Format
- If no valid assignment exists, print
NO. - Otherwise print
YESon the first line, followed by one line with integers describing a valid assignment.
Constraints
- All assigned values must be integers.
- For each day , the chosen value must satisfy .
- The total sum must equal .
Exact numeric bounds are not provided in the source metadata, but the intended solution is linear or near-linear in .
Example 1
Input
3 10 1 3 2 5 1 4
Output
YES 3 3 4
Explanation
The minimum total is 1+2+1=4, leaving 6 extra hours to distribute. One valid distribution is 3, 3, and 4, which sums to 10 and stays within all ranges.
Example 2
Input
2 1 2 3 1 2
Output
NO
Explanation
The minimum total is 2+1=3, which is already larger than the required sum 1.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.