#C5714. Balanced Candy Distribution

    ID: 49394 Type: Default 1000ms 256MiB

Balanced Candy Distribution

Balanced Candy Distribution

A company has M candies and N employees. The goal is to achieve a balanced distribution where the difference between the employee with the maximum candies and the one with the minimum candies is at most 1.

You can perform the following operations:

  • Add one candy.
  • Remove one candy.
  • Add one employee.
  • Remove one employee (note: there must be at least one employee remaining).

The challenge is to determine the minimum number of operations required to achieve such a balanced distribution.

In mathematical terms, if each employee eventually gets either x or x+1 candies, then the absolute difference is at most 1. You need to transform the current situation (with M candies and N employees) into a balanced state with the minimum number of operations.

Note: When the number of candies is less than the number of employees, adding candies (or reducing the number of employees) might be necessary.

All formulas are given in LaTeX format where needed. For instance, the remainder operation is expressed as: \(r = M \bmod N\) and the answer is \(\min(r, N - r)\) when \(M \geq N\).

inputFormat

The input is read from the standard input (stdin) and consists of a single line with two space-separated integers:

  • M: The total number of candies.
  • N: The total number of employees.

outputFormat

Output to the standard output (stdout) a single integer representing the minimum number of operations required to achieve a balanced candy distribution.

## sample
52 10
2