The article describes a Python-based algorithm for distributing a fixed delivery cost across multiple boxes of varying weights. The primary challenge addressed is the rounding error that occurs when individual shares are rounded separately, leading to a discrepancy with the original total.

The proposed method involves calculating the integer part of each share first. Remaining fractional cents are then distributed to boxes with the largest remainders. To ensure consistent results regardless of input order, the algorithm uses box identifiers as a tie-breaker for identical remainders.

The algorithm utilizes the divmod function to divide the product of the total cost and individual weight by the total weight. This approach ensures that the sum of the calculated shares exactly matches the original total cost, effectively eliminating rounding discrepancies.

Testing included 12,524 scenarios with weights ranging from 0 to 4 and costs from 0 to 100 kopecks. The algorithm successfully handled cases involving zero-weight boxes, identical weights, and key reordering, consistently maintaining a zero difference between the calculated sum and the original total.