<\/span><\/h2>\n\n\n\nTomorrow Peter has a Biology exam. He does not like this subject much, but d<\/em> 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 minTime<\/em>i<\/em><\/sub> and not more than maxTime<\/em>i<\/em><\/sub> hours per each i<\/em>-th day. Moreover, they warned Peter that a day before the exam they would check how he has followed their instructions.<\/p>\n\n\n\nSo, 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<\/em> spent him on preparation, and now he wants to know if he can show his parents a timetable s\u0441hedule<\/em> with d<\/em> numbers, where each number s\u0441hedule<\/em>i<\/em><\/sub> stands for the time in hours spent by Peter each i<\/em>-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of all schedule<\/em>i<\/em><\/sub> should equal to sumTime<\/em>.<\/p>\n\n\n\n<\/div>\n\n\n\n
Input<\/strong><\/p>\n\n\n\nThe first input line contains two integer numbers d<\/em>,\u2009sumTime<\/em> (1\u2009\u2264\u2009d<\/em>\u2009\u2264\u200930,\u20090\u2009\u2264\u2009sumTime<\/em>\u2009\u2264\u2009240) \u2014 the amount of days, during which Peter studied, and the total amount of hours, spent on preparation. Each of the following d<\/em> lines contains two integer numbers minTime<\/em>i<\/em><\/sub>,\u2009maxTime<\/em>i<\/em><\/sub> (0\u2009\u2264\u2009minTime<\/em>i<\/em><\/sub>\u2009\u2264\u2009maxTime<\/em>i<\/em><\/sub>\u2009\u2264\u20098), separated by a space \u2014 minimum and maximum amount of hours that Peter could spent in the i<\/em>-th day.<\/p>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nIn the first line print YES, and in the second line print d<\/em> numbers (separated by a space), each of the numbers \u2014 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.<\/p>\n\n\n\n<\/div>\n\n\n\n
Examples<\/strong><\/p>\n\n\n\nInput<\/td> 1 48 5 7<\/td><\/tr> Output<\/td> NO<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/div>\n\n\n\n
Input<\/td> 2 5 0 1 3 5<\/td><\/tr> Output<\/td> YES 1 4<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/div>\n\n\n\n
\n\n\n\n
<\/span>Solution<\/strong><\/span><\/h2>\n\n\n\nIn our solution, we will use two loop with many calculate to solve this problem.<\/p>\n\n\n\n
<\/div>\n\n\n\n
<\/span>C# Solution<\/strong><\/span><\/h3>\n\n\n\nSolution1<\/strong><\/p>\n\n\n\nstring[] first = Console.ReadLine().Split(' '); \/\/split by space\nint d = int.Parse(first[0]); \/\/days\nint sumTime = int.Parse(first[1]); \n\nint[]minArr = new int[d]; \/\/min value also use as the result\nint[]leftArr = new int[d];\n\nfor(int i=0;i<d;i++)\n{\n var arr = Console.ReadLine().Split(' ');\n var min = int.Parse(arr[0]); \n var max = int.Parse(arr[1]); \n \n sumTime-=min;\n minArr[i] = min;\n leftArr[i] = max-min;\n}\n\nif(sumTime==0){\n Console.WriteLine("YES");\n Console.WriteLine(String.Join(" ", minArr));\n}\nelse if(sumTime<0){\n Console.WriteLine("NO"); \n}\n\/\/still need more time\nelse{\n for(int i=0;i<d;i++)\n {\n if(sumTime>leftArr[i]){\n sumTime -= leftArr[i];\n minArr[i]+=leftArr[i];\n }\n else{\n minArr[i]+=sumTime;\n sumTime = 0;\n Console.WriteLine("YES");\n Console.WriteLine(String.Join(" ", minArr));\n break;\n }\n }\n}\n\n\/\/still need more time!!\nif(sumTime>0){\n Console.WriteLine("NO"); \n}<\/code><\/pre><\/div>\n\n\n\nIn 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. <\/p>\n\n\n\n
Let me explain why minArr is referred to as “also used as the result”:<\/p>\n\n\n\n
\nStoring Minimum Time: <\/strong>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.<\/li>\n\n\n\nResult<\/strong>: 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.<\/li>\n<\/ul>\n\n\n\n<\/div>\n\n\n\n
<\/span>Java Solution<\/strong><\/span><\/h3>\n\n\n\nSolution1<\/strong><\/p>\n\n\n\nimport java.util.Scanner;\nimport java.util.Arrays;\n\npublic class Main {\n public static void main(String[] args) {\n Scanner scanner = new Scanner(System.in);\n String[] first = scanner.nextLine().split(" ");\n \n int d = Integer.parseInt(first[0]); \/\/days\n int sumTime = Integer.parseInt(first[1]); \n \n int[]minArr = new int[d]; \/\/min value also use as the result\n int[]leftArr = new int[d];\n \n for(int i=0;i<d;i++)\n {\n String[] arr = scanner.nextLine().split(" ");\n int min = Integer.parseInt(arr[0]); \n int max = Integer.parseInt(arr[1]); \n \n sumTime-=min;\n minArr[i] = min;\n leftArr[i] = max-min;\n }\n \n if(sumTime==0){\n System.out.println("YES");\n System.out.println(String.join(" ", Arrays.stream(minArr).mapToObj(String::valueOf).toArray(String[]::new)));\n }\n else if(sumTime<0){\n System.out.println("NO"); \n }\n \/\/still need more time\n else{\n for(int i=0;i<d;i++)\n {\n if(sumTime>leftArr[i]){\n sumTime -= leftArr[i];\n minArr[i]+=leftArr[i];\n }\n else{\n minArr[i]+=sumTime;\n sumTime = 0;\n System.out.println("YES");\n System.out.println(String.join(" ", Arrays.stream(minArr).mapToObj(String::valueOf).toArray(String[]::new)));\n break;\n }\n }\n }\n \n \/\/still need more time!!\n if(sumTime>0){\n System.out.println("NO"); \n }\n }\n}<\/code><\/pre><\/div>\n\n\n\nIn Java, the String.join<\/em><\/strong> function requires a string array as input, so we need to convert minArr <\/strong>to a string array before printing it.<\/p>\n\n\n\n<\/div>\n\n\n\n