CodeforcesMay 04, 2025

Commentary Boxes

Hazrat Ali

Codeforces

Organizers have already built nn commentary boxes. mm 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.

If nn is not divisible by mm, it is impossible to distribute the boxes to the delegations at the moment.

Organizers can build a new commentary box paying aa burles and demolish a commentary box paying bb 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.

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 mm)?

Input

The only line contains four integer numbers nnmmaa and bb (1n,m10121≤n,m≤10121a,b1001≤a,b≤100), where nn is the initial number of the commentary boxes, mm is the number of delegations to come, aa is the fee to build a box and bb is the fee to demolish a box.

Output

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 mm). It is allowed that the final number of the boxes is equal to 00.

Examples
Input
9 7 3 8
Output
15
Input
2 7 3 7
Output
14
Input
30 6 17 19
Output
0


Solution
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long n, m, a, b;
  cin >> n >> m >> a >> b;
  long long ac, bc;
  if (n % m == 0) {
    cout << 0 << endl;
  } else if (n > m) {
    ac = (n / m + 1) * m - n;
    bc = n % m;
    cout << min(ac * a, bc * b) << endl;
  } else {
    ac = m - n;
    bc = n;
    cout << min(ac * a, bc * b) << endl;
  }
  return 0;
}



Comments