暑假训练第一场L

题目:

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
9 7 3 8
Output
15
Input
2 7 3 7
Output
14
Input
30 6 17 19
Output
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个房子,建一个房子是a元,拆房子是b元。求出最小值。

源码:

#include
using namespace std;
int main()
{
	long long n, m, a, b, min,jian;
	cin >> n >> m >> a >> b;
	if (n%m == 0)cout << "0" << endl;
	else
	{
		min = (n%m)*b;
		jian= ((n/m+1)*m-n)*a;
		if (jian <= min)min = jian;
		cout << min << endl;
	}
	return 0;
}


你可能感兴趣的:(农大暑假训练)