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 (
1≤n,m≤1012,
1≤a,b≤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.
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
#include
#include
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
",min(n%m*b,(m-n%m)*a)); return 0; }

좋은 웹페이지 즐겨찾기