Equalise an Array
The Weekly Challenge 270/2
In the week 270, the second task was really interesting and difficult. Here’s a slightly reformulated version:
We’re given an array of positive integers@intsand two additional integers,$xand$y. We can apply any sequence of the following two operations: 1. Increment one element of@ints. 2. Increment two elements of@ints. The cost of each application of operation 1 is$x, the cost of operation 2 is$y. What’s the minimal cost of a sequence of operations that makes all the elements of@intsequal?
Why do I say it was difficult? I compared all the Perl and Raku solutions I could find in the GitHub repository and none of them gave the same results as mine. It took me several days to find an algorithm that would answer the tricky inputs I generated with a pen and paper, and one more day to optimise it to find the solutions in a reasonable time.