A reactor facility stores fuel rods that must be packaged in batches: a batch is only accepted for storage if the total remaining length of all its rods is exactly divisible by the facility's required batch size k. You are given the current lengths of n fuel rods. In one operation, you may select any single rod and trim exactly 1 unit off its length (a rod's length may be trimmed all the way down to 0). Determine the minimum number of trim operations needed so that the sum of all rod lengths becomes divisible by k.
The first line contains two integers n and k.
The second line contains n integers, the lengths of the rods.
Print a single integer: the minimum number of trim operations required.
1 <= n <= 1000001 <= k <= 10^91 <= rod length <= 10^9Example 1
Input
3 5 3 7 10
Expected
0
Explanation
The sum of the rod lengths is 3+7+10=20, and 20 is already divisible by 5, so no trims are needed and the answer is 0.
Example 2
Input
3 4 1 2 3
Expected
2
Explanation
The sum is 1+2+3=6, and 6 mod 4 = 2. Trimming any rods by a total of 2 units (for example the third rod twice, giving lengths [1,2,1] summing to 4) makes the total divisible by 4, and fewer than 2 trims cannot work since 6-1=5 is not divisible by 4. The answer is 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →