990A.md (2149B)
1 Source: https://codeforces.com/problemset/problem/990/A 2 3 A. Commentary Boxes 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Berland Football Cup starts really soon! Commentators from all over the world come to the event. 22 23 Organizers have already built $n$ commentary boxes. $m$ regional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation. 24 25 If $n$ is not divisible by $m$, it is impossible to distribute the boxes to the delegations at the moment. 26 27 Organizers can build a new commentary box paying $a$ burles and demolish a commentary box paying $b$ burles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes. 28 29 What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by $m$)? 30 31 Input 32 33 The only line contains four integer numbers $n$, $m$, $a$ and $b$ ($1 \le n, m \le 10^{12}$, $1 \le a, b \le 100$), where $n$ is the initial number of the commentary boxes, $m$ is the number of delegations to come, $a$ is the fee to build a box and $b$ is the fee to demolish a box. 34 35 Output 36 37 Output the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by $m$). It is allowed that the final number of the boxes is equal to $0$. 38 39 Examples 40 41 Input 42 43 ``` 44 9 7 3 8 45 ``` 46 47 Output 48 49 ``` 50 15 51 ``` 52 53 Input 54 55 ``` 56 2 7 3 7 57 ``` 58 59 Output 60 61 ``` 62 14 63 ``` 64 65 Input 66 67 ``` 68 30 6 17 19 69 ``` 70 71 Output 72 73 ``` 74 0 75 ``` 76 77 Note 78 79 In the first example organizers can build $5$ boxes to make the total of $14$ paying $3$ burles for the each of them. 80 81 In the second example organizers can demolish $2$ boxes to make the total of $0$ paying $7$ burles for the each of them. 82 83 In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get $5$ boxes.