codeforce 990A

题目描述:

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

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.

题目大意:

 首先输入4个数,第一个数代表多少个凳子,第二个数有几支队伍,第三个数是增加凳子所需要的耗费,第四个数是拿走凳子所需要的耗费,问在满足每支队伍得到凳子相同的情况下,所需的的最小消费。假如刚开始时,便可以均分凳子,则输出0.

如题目中的例子:例一,有九个凳子,七支队伍,要么减去两张凳子,耗费2*8=16,要么增加五个凳子,3*5=15,显然增加五张凳子划算。  例二:有两张凳子,七支队伍,减去两张凳子耗费2*7=14,增加五张凳子耗费3*5=15,显然选择减去两张凳子。(可以每支队伍都没有)

下方的代码无论代码一还是代码二,只要理清增减凳子所用的消费比较关系即可。注意:这里n,m使用long long,不然会出错

(自己做时应scanf中只输入了ld,少了一个l废了很多时间挑错)

代码1:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
const int maxn=500000;
int main()
{
   int a, b;
   LL n ,m ,sum;
   while(scanf("%lld%lld%d%d",&n,&m,&a,&b)!=EOF)
   {
       LL k,sum;
       if(n%m==0)cout<<0<<endl;
          else
          {
             if(n<m) sum=min((m-n)*a,n*b);
              else if(n>m)
              {
                 k=n/m;
                 LL x=n-m*k;
                 //上方两句的作用相当于k=n%m,下方把x换为k即可
                  sum=min((m-x)*a,x*b);
              }
              cout<<sum<<endl;
          }
   }

   return 0;
}

代码二:(更简洁)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define LL long long
using namespace std;
const int maxn=500000;
int main()
{
   int  a, b;
   LL n ,m ,k;
   while (scanf("%lld%lld%d%d",&n,&m,&a,&b)!=EOF)
   {
         LL sum=min((m-(n % m) )*a,(n % m)*b);
            cout<<sum<<endl;
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/fanxingxue/article/details/82783128
今日推荐