Codeforces 4B Before an Exam Solution & Explanation

Difficulty : 1200

Problem Description

Tomorrow Peter has a Biology exam. He does not like this subject much, but d days ago he learnt that he would have to take this exam. Peter’s strict parents made him prepare for the exam immediately, for this purpose he has to study not less than minTimei and not more than maxTimei hours per each i-th day. Moreover, they warned Peter that a day before the exam they would check how he has followed their instructions.

So, today is the day when Peter’s parents ask him to show the timetable of his preparatory studies. But the boy has counted only the sum of hours sumTime spent him on preparation, and now he wants to know if he can show his parents a timetable sсhedule with d numbers, where each number sсhedulei stands for the time in hours spent by Peter each i-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of all schedulei should equal to sumTime.

Input

The first input line contains two integer numbers d, sumTime (1 ≤ d ≤ 30, 0 ≤ sumTime ≤ 240) — the amount of days, during which Peter studied, and the total amount of hours, spent on preparation. Each of the following d lines contains two integer numbers minTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in the i-th day.

Output

In the first line print YES, and in the second line print d numbers (separated by a space), each of the numbers — amount of hours, spent by Peter on preparation in the corresponding day, if he followed his parents’ instructions; or print NO in the unique line. If there are many solutions, print any of them.

Examples

Input1 48
5 7
OutputNO
Input2 5
0 1
3 5
OutputYES
1 4

Solution

In our solution, we will use two loop with many calculate to solve this problem.

C# Solution

Solution1

string[] first = Console.ReadLine().Split(' '); //split by space
int d = int.Parse(first[0]);    //days
int sumTime = int.Parse(first[1]); 

int[]minArr = new int[d];   //min value also use as the result
int[]leftArr = new int[d];

for(int i=0;i<d;i++)
{
    var arr = Console.ReadLine().Split(' ');
    var min = int.Parse(arr[0]);  
    var max = int.Parse(arr[1]);  
    
    sumTime-=min;
    minArr[i] = min;
    leftArr[i] = max-min;
}

if(sumTime==0){
    Console.WriteLine("YES");
    Console.WriteLine(String.Join(" ", minArr));
}
else if(sumTime<0){
        Console.WriteLine("NO"); 
}
//still need more time
else{
    for(int i=0;i<d;i++)
    {
        if(sumTime>leftArr[i]){
            sumTime -= leftArr[i];
            minArr[i]+=leftArr[i];
        }
        else{
            minArr[i]+=sumTime;
            sumTime = 0;
            Console.WriteLine("YES");
            Console.WriteLine(String.Join(" ", minArr));
            break;
        }
    }
}

//still need more time!!
if(sumTime>0){
    Console.WriteLine("NO"); 
}

In this code, the minArr array is used to store the minimum time Peter spends studying biology each day, and it is also used as the final result.

Let me explain why minArr is referred to as “also used as the result”:

  • Storing Minimum Time: In the first loop, minArr is used to store the minimum time Peter spends studying biology each day. This is achieved by the line of code minArr[i] = min;, which stores the minimum time for each day in the minArr array.
  • Result: After satisfying all the conditions, if sumTime becomes 0, then minArr represents a valid timetable because it contains the minimum time for each day. Therefore, when the conditions are met, you choose to use minArr to represent the final result, i.e., Peter’s timetable for studying biology.

Java Solution

Solution1

import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] first  = scanner.nextLine().split(" ");
        
        int d = Integer.parseInt(first[0]);    //days
        int sumTime = Integer.parseInt(first[1]); 
        
        int[]minArr = new int[d];   //min value also use as the result
        int[]leftArr = new int[d];
        
        for(int i=0;i<d;i++)
        {
            String[] arr = scanner.nextLine().split(" ");
            int min = Integer.parseInt(arr[0]);  
            int max = Integer.parseInt(arr[1]);  
            
            sumTime-=min;
            minArr[i] = min;
            leftArr[i] = max-min;
        }
        
        if(sumTime==0){
            System.out.println("YES");
            System.out.println(String.join(" ", Arrays.stream(minArr).mapToObj(String::valueOf).toArray(String[]::new)));
        }
        else if(sumTime<0){
                System.out.println("NO"); 
        }
        //still need more time
        else{
            for(int i=0;i<d;i++)
            {
                if(sumTime>leftArr[i]){
                    sumTime -= leftArr[i];
                    minArr[i]+=leftArr[i];
                }
                else{
                    minArr[i]+=sumTime;
                    sumTime = 0;
                    System.out.println("YES");
                    System.out.println(String.join(" ", Arrays.stream(minArr).mapToObj(String::valueOf).toArray(String[]::new)));
                    break;
                }
            }
        }
        
        //still need more time!!
        if(sumTime>0){
            System.out.println("NO"); 
        }
    }
}

In Java, the String.join function requires a string array as input, so we need to convert minArr to a string array before printing it.


Conclusion

This problem is the second problem of codeforces contest – Beta Round 4

You can find the solution for the first problem here – Codeforces 4A Watermelon Solution & Explanation

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 4B – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *