Ilya and Diplomas
Practice this coding question on Coding Gurukul.
Problem statement
Ilya is preparing diploma envelopes for n participants at a coding exhibition. Every participant must receive one diploma, and there are three award levels. The printing office has approved a minimum and maximum quantity for each level, so the final order must respect all three ranges. Your task Choose counts x1, x2 and x3 within their respective inclusive ranges such that x1 + x2 + x3 = n. First maximize x1. Among distributions with that value of x1, maximize x2. The remaining participants receive level-three diplomas. Important details All participants receive exactly one diploma, and no printed diploma is left over. Respect the minimum for a lower level even while maximizing a higher level. The input always permits at least one distribution. The priority order determines a unique answer, so do not print an arbitrary feasible distribution. Worked example For n = 12 and ranges [2, 7], [3, 6], [1, 4], reserve at least three level-two and one level-three diplomas. Seven level-one diplomas are possible. Of the remaining five diplomas, assign four to level two and one to level three. The required output is 7 4 1. When n equals the sum of the three minimums, those minimums are the only possible counts.
Input format
First line: n. Next three lines: minimum and maximum counts for levels one, two and three, in that order.
Output format
Print x1 x2 x3 for the distribution selected by the stated priority order.
Constraints
3 <= n <= 3000000; 1 <= minimum[i] <= maximum[i] <= 1000000; sum(minimum) <= n <= sum(maximum).
Sample input
12 2 7 3 6 1 4
Sample output
7 4 1
Sign in to run or submit a solution in C++, Java or Python.