8月5号团队赛补题

 1.Commentary Boxes

Description

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 (1n,m10121≤n,m≤1012, 1a,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.

Sample Input

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

Hint

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支代表队,可以新建一些评论框代价是a,也可以拆除一些评论框代价是b。问最后至少花费多少。

解题思路:分别算出新建一些和拆除一些评论框的花费,找到最少的花费就行了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long int
using namespace std;
int main()
{
    ll n,m,a,b,x,y,ans1,ans2,ans;
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    x=n%m;
    y=n/m;
    ans1=x*b;///
    ans2=((y+1)*m-n)*a;//
    ans=min(ans1,ans2);
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wkfvawl/p/9426463.html
今日推荐