A. Commentary Boxes

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jj12345jj198999/article/details/82560633

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Berland Football Cup starts really soon! Commentators from all over the world come to the event.

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 nn, mm, aa and bb (1≤n,m≤10121≤n,m≤1012, 1≤a,b≤1001≤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

Copy

9 7 3 8

output

Copy

15

input

Copy

2 7 3 7

output

Copy

14

input

Copy

30 6 17 19

output

Copy

0

Note

In the first example organizers can build 55 boxes to make the total of 1414 paying 33 burles for the each of them.

In the second example organizers can demolish 22 boxes to make the total of 00 paying 77 burles for the each of them.

In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get 55 boxes.

解题说明:此题其实就是判断n是否为m的倍数,如果不是需要增加或减少多少才满足条件,求出余数进行判断即可。

#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

int main() 
{
	long long n, m, a, b, c, d, e, k, l;
	scanf("%lld%lld%lld%lld", &n, &m, &a, &b);
	c = n % m;
	if (c == 0)
	{
		printf("0");
	}
	else
	{
		d = m - c;
		k = d * a;
		l = c * b;
		if (k>l)
		{
			printf("%lld\n", l);
		}
		else
		{
			printf("%lld\n", k);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/jj12345jj198999/article/details/82560633
今日推荐