1. Task counts and days
Let be the common final progress and the number of days. Task must be selected times. The following conditions are necessary and sufficient:
Necessity is immediate. For sufficiency, when days remain, choose the tasks with the largest remaining counts. At least counts are positive, and at most counts equal , so every count equal to is selected. Decrementing the selected counts leaves total and maximum at most . Repetition constructs a valid schedule.
2. Bounding the final progress
Set , , and . Progress cannot decrease, and each task is selected at most once per day, so any solution with satisfies . By the input promise, if no solution with exists in this interval, the answer is impossible. The bound alone does not guarantee , so the day count must also be checked.
3. Generalized CRT
Integrality requires for every task. Maintain the merged congruence , with , initially . Substituting into the next congruence gives .
Let . If does not divide , there is no solution. Otherwise, with , compute
with , then update and . When , use . Compute the inverse with the extended Euclidean algorithm.
If during merging, the only possible value in is . Stop merging and directly check against the entire input: congruences, range, total count, maximum count, and day limit. Congruences not yet merged must also be checked.
4. Linear expressions and lower bounds
If all congruences have been merged and , write . Define integers
Then and . Since , the day count increases with . Start with .
The condition becomes . If , this gives the lower bound
Update with these bounds. Leave the other tasks for the final candidate check. Ceiling division must work correctly for negative numerators as well.
5. The first congruence candidate
The total count must be divisible by , so solve . Let . If does not divide , there is no solution. Otherwise divide by and use a modular inverse to obtain , where and . For , set . The first candidate at least is
First check . If this fails, report impossibility without performing the large multiplication. Otherwise compute and directly verify all original conditions.
6. Why one candidate is enough
All conditions with were included in the lower bound. For every other task, is nondecreasing, so if its inequality fails at the first candidate, it fails at every later candidate. The day count also increases; if the first candidate has , later candidates cannot help. Thus a successful first candidate is optimal, and a failed first candidate implies impossibility.
7. Integer bounds and complexity
We have , and merging only while gives . Reduce residues before multiplying by an inverse. The sums and lower-bound numerators are safely bounded in absolute value by . Multiply the candidate by only after checking its range, then recompute counts from to obtain the day count. Every intermediate value in this implementation therefore fits a signed -bit integer. Do not compute the unrestricted LCM or multiply an out-of-range candidate by .
With and fixed-width arithmetic, the time complexity is and the space complexity is .
8. Subtask solutions
In subtask 1, . Set , , and . The conditions become and . If , only an initially equal array is possible. Otherwise find the lower bound and enumerate the residue of from to , giving time.
In subtask 2, enumerate final progress from through and check the necessary and sufficient conditions. Since , , and , the first candidate above has and the minimum feasible progress is below . The enumeration is therefore sufficient and takes time. Subtask 3 uses the full solution.
Solution written by GPT