990A. Commentary Boxes(思维)

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

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.

If n

is not divisible by m

, it is impossible to distribute the boxes to the delegations at the moment.

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.

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

)?

Input

The only line contains four integer numbers n

, m, a and b ( 1n,m1012, 1a,b100), 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.

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 m

). It is allowed that the final number of the boxes is equal to 0

.

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 5

boxes to make the total of 14 paying 3

burles for the each of them.

In the second example organizers can demolish 2

boxes to make the total of 0 paying 7

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 5

boxes.

题意:在世界杯即将举行,将有m支球队来参加,已经做好了n件纪念品发放给球队,每支球队的纪念品数量必须相等或者均为0,为了满足这一条件,必须购买增加纪念品数量(成本是每件a元),或者销毁部分纪念品(成本是每件b元),求举办方最少花费多少钱使得纪念品能够被球队平分。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
int main(){
	LL n,m,a,b;
	scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
	printf("%lld\n",min(n%m*b,(m-n%m)*a));
	return 0;
}


猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/81010493
今日推荐