We are given: n, m, a, b, all up to 1e12 for n, m, and a, b up to 100. Need minimal cost to make n divisible by m (i.e., n' such that m | n'), with allowed operations: build new box: cost a per box; demolish a box: cost b per box. Can do any number of builds and demolishes, paying each time. The final number can be 0, which is divisible by m (since 0 is divisible by any m? Typically 0 is divisible by any positive integer because 0 = m * 0, so yes, it's allowed). So we can reduce n to 0, costing nb. Or we can increase n to some multiple of m, costing (km - n)a if km >= n, where k = ceil(n/m) or maybe larger multiples (like if building more than necessary could be cheaper? Actually building costs a per box. So building up to the next multiple is the minimal building cost to get to a multiple. But could building beyond that be cheaper? No, because building more boxes only increases cost, and we need exactly divisible; if we go to a higher multiple, we pay more building cost than to the nearest multiple above n. However, there is also option to demolish to reach a lower multiple, perhaps demolishing to 0 or to some multiple below n. So we have two basic strategies: either increase n to the next multiple of m (call k_up = ceil(n/m) * m) costing (k_up - n)*a; or decrease n to the previous multiple (including 0) (k_down = floor(n/m) * m) costing (n - k_down)b. But also we could consider demolishing all boxes to 0, which is k_down when floor(n/m)=0 if n < m. Or building from 0? No we start from n; we can both build and demolish. Could a combination of both be cheaper? For example, if a is small and b is small, maybe it's cheaper to demolish some boxes and build some others to reach a different multiple? But the final number must be some multiple of m, call N = km. The cost to go from n to N is: if N >= n: cost = (N - n)a; if N <= n: cost = (n - N)b. If N could be either larger or smaller, then the cost function is piecewise linear. But note we can also do both build and demolish? Actually if we do both, it's wasteful: suppose we demolish some and then build some to reach a different multiple, the total cost would be sum of both operations; that is equivalent to moving to a number N that is either above or below n? Because any sequence of builds and demolishes that ends at N yields net change = N - n. If you both demolish and build, the net change is N - n, but you paid for both operations. For example, you demolish x boxes and build y boxes, final = n - x + y = N => y - x = N - n. The cost = xb + ya. Since x and y are non-negative integers, cost = (if N - n is positive, you can achieve it by only building; if negative, only demolishing). But if you do both, you could have x>0, y>0. But is that ever cheaper? Suppose N - n is positive, you could just build y = N - n boxes, cost = (N - n)*a. If you also demolish some boxes and then build more to compensate, you would increase cost because you pay for demolishing extra and building extra. However, there might be a scenario where N is not directly reachable by only building or only demolishing? Actually any N is reachable by either building (if N >= n) or demolishing (if N <= n). So doing both would only add extra cost. So we only need to consider final multiples either >= n (cost via building) or <= n (cost via demolishing). So the minimal cost is min( cost to go to nearest multiple above, cost to go to nearest multiple below, maybe also to 0? That's included in the multiples below as floor(n/m)*m. But what about multiples that are not the immediate ones? If we consider going up to a multiple higher than the immediate next one, the building cost increases because the difference is larger. So that's not better. Similarly, going down to a multiple lower than the immediate previous (including 0) might not be better? Actually floor(n/m)*m is the highest multiple less or equal to n. Going to a lower multiple (like floor(n/m)-1 * m) would cost more demolishing because you need to demolish more boxes. So not better. Therefore the minimal cost is simply min( (ceil(n/m)*m - n) * a, (n - floor(n/m)m) * b ). However, we have to consider that floor(n/m) could be 0, and then n - 0 = n, cost = nb (demolish all). That is included.
But wait: Are there cases where both building and demolishing simultaneously could lead to a cheaper cost due to something like building cost a being small but demolishing cost b being also small? But we already saw if we aim for N > n, we would only build. If we aim for N < n, we only demolish. Could there be a scenario where N is such that it is cheaper to demolish some and then build to N, but N is between n and some multiple? No, N must be multiple of m. So if we want to go to a multiple that is > n, we only build. If we want to go to a multiple that is < n, we only demolish. There's no need to both.
Thus answer seems straightforward.
But let's test examples:
Example1: n=9,m=7,a=3,b=8. ceil(9/7)=2 => 14-9=5 -> cost=53=15. floor(9/7)=1 =>9-7=2 -> cost=28=16. min=15 -> output 15. matches.
Example2: n=2,m=7,a=3,b=7. ceil(2/7)=1 =>7-2=5 -> cost=53=15. floor(2/7)=0 =>2-0=2 -> cost=27=14. min=14 -> output 14. matches.
Example3: n=30,m=6,a=17,b=19. 30 mod 6 =0, so both differences are 0 => cost 0. matches.
Seems trivial. But wait, constraints: n,m up to 1e12, a,b up to 100. Simple calculation works. However, I recall a known Codeforces problem "Berland Football" maybe with a twist? I think there is a nuance: The problem states "It is allowed that the final number of the boxes is equal to 0." Indeed, 0 is multiple of m. So floor(n/m)*m might be 0 if n < m. That's covered.
But some might think about negative cost? No.
But what about the possibility of doing both building and demolishing to achieve a multiple that is not the nearest? Could there be a case where building some and demolishing some results in lower total cost because building cost a is cheaper than demolishing cost b, but the nearest multiple above requires building many, while the nearest multiple below requires demolishing many? Actually the cost to go up is (k_up - n)*a. Cost to go down is (n - k_down)*b. But maybe there is a multiple further up that is not the immediate next but has a lower cost due to a being small? No, if a is positive, increasing the difference increases cost. If a is zero? But a >= 1 per constraints, so positive.
Similarly, if b is zero? b >=1.
So no.
But perhaps there is a nuance: If n is very large, and m is also large, but we need to compute ceil(n/m) and floor(n/m). That can be done as: r = n % m, then up = (m - r) % m? Actually the difference to next multiple: if r == 0, cost_up = 0; else cost_up = (m - r)a. Cost down = rb. So answer = min( (m - r)a, rb ) where r = n % m. But careful: If n = 0? n >= 1 per constraints? It says 1 ≤ n, m ≤ 10^{12}. So n >=1. But still, r = n%m is defined. If n < m, then r = n, up cost = (m - n)a, down cost = nb. Works.
Thus solution: compute r = n % m, answer = min( r*b, (m - r)*a )? But check: if r = 0, both 0. Good.
Wait: For n=9,m=7: r=2 => up cost = (7-2)a=53=15, down cost =2*8=16 -> min 15. matches.
For n=2,m=7: r=2 => up = (7-2)3=53=15, down =2*7=14 -> min14.
So answer formula is straightforward.
But is there any case where m - r is zero? That would be when r=0, then up cost=0 as well.
Thus answer is min(r*b, (m-r)*a).
But must consider that we might demolish all boxes to 0, but 0 is also a multiple of m. In our formula, floor(n/m)*m = (n//m)m. If n < m, floor = 0, and r = n, so down cost = nb. That's demolishing all to 0. Good.
So the answer is simply min( (n % m) * b, (m - (n % m)) % m * a )? Actually (m - (n%m)) % m yields 0 when n%m=0, else m - r.
Thus final answer = min( r * b, (m - r) * a ), with careful about using modulo for large numbers but fine.
But there might be an edge case: m up to 1e12, r can be up to m-1, and b up to 100, so r*b can be up to 1e12 * 100 = 1e14, which fits in 64-bit (2^63-1 ~9e18). So we can use 64-bit (long long in C++). Python integers unlimited.
Thus problem seems extremely easy. But why such high constraints? Perhaps there is a catch: The organizers can build new boxes and demolish existing boxes any number of times, but each time they pay the fee. Could there be a scenario where it is cheaper to both demolish and build to reach a multiple that is not simply above or below? Let's analyze more formally.
We start at n. We can perform operations: add 1 (cost a) or subtract 1 (cost b). We want to end at some N such that N % m == 0 and N >= 0 (presumably integer). Cost is total cost of adds plus total cost of subtracts. The net change = N - n. If we do some adds and some subtracts, the total cost = (num_adds)*a + (num_subs)*b. Since num_adds - num_subs = N - n, we can write cost = (N - n + num_subs)a + num_subsb = (N - n)a + num_subs(a+b). If N >= n, then we could set num_subs = 0 to achieve cost = (N-n)*a. Any positive num_subs would increase cost because a+b > 0. Similarly, if N <= n, we can set num_adds = 0, cost = (n-N)*b. And if we set both positive, cost = (n-N)b + num_adds(a+b) for N < n, etc. So it's never optimal to do both. So indeed only pure add or pure subtract.
Thus answer is as simple.
But wait: There might be a scenario where N could be 0, which is less than n, cost = n*b. That's covered by down cost when floor(n/m)=0. But what about N = m? That is greater than n if n < m; up cost = (m-n)*a. So min between these.
Now, is there any other possibility? Could we consider demolishing all boxes and then building some? That would be a combination, which we argued is suboptimal. For example, if a is very cheap and b is expensive, it might be cheaper to build up to a multiple even though we are above? Actually if n > m, maybe it's cheaper to demolish to a lower multiple? That's accounted by rb. But if n is huge, maybe it's cheaper to build up to the next multiple if r is small and a is cheap, else demolish down if rb is small. That's our min.
Thus answer is trivial. Perhaps the problem expects the participants to realize this simple solution, but the high constraints are to ensure they don't try brute force over multiples.
But wait, the problem statement: "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." So it's exactly as we think.
Thus the solution is straightforward.
But let me think if there could be an alternative: Maybe it is possible to achieve divisibility by making the number of boxes negative? No, negative not allowed. Final number can be 0 or positive. So fine.
Thus the solution code: read n,m,a,b; compute r = n % m; compute cost_up = (m - r) * a; but careful: if r == 0, then m - r = m, which would be ma. That would not be correct because if n is divisible by m, we don't need to build anything. Actually if r=0, then n is already divisible, so we don't need to do anything. So cost_up should be 0, not ma. Our formula earlier used (m - r)a, but when r=0, that yields ma which is not minimal; but we would take min with rb=0, so answer 0. So using min( rb, (m - r)a ) works because when r=0, (m - r)a = ma, but min with 0 yields 0. So fine. However, there is a nuance: When n% m == 0, we should output 0, not ma. So min does that. So okay.
But what about if we compute cost_up = (m - r) % m * a? For r=0, (m - 0) % m = 0, because m % m = 0? Actually m % m = 0. So (m - r) % m yields 0 when r=0. So that is a better approach: cost_up = ((m - r) % m) * a. That yields 0 when r=0. Then answer = min( r*b, ((m - r) % m)*a ). That works as well.
But it's simpler to do: if r==0: ans=0 else ans = min(r*b, (m-r)*a).
Now check for potential overflow: m and n up to 1e12, a,b up to 100, so r up to ~1e12, product up to 1e14, fits in 64-bit. In Python no issue.
Thus solution.
But wait, the sample 2: n=2,m=7,r=2, (m-r)=5, cost_up=15, cost_down=14, min=14.
So seems good.
But what about n=0? The constraints say n>=1, but even if n=0, then r=0, ans=0.
Thus solved.
But is there any hidden trick? Perhaps they consider that you can demolish and build simultaneously to achieve a multiple that is not directly above or below, but we already argued it's not beneficial. However, consider if a+b is negative? No costs are positive.
Thus the answer is straightforward.
Nevertheless, let's try to think of some edge case: n=5, m=10, a=1, b=100. r=5. up cost = (10-5)1=5. down cost =5100=500. So build 5 boxes cost 5. That's obviously min. So we build up.
n=5, m=10, a=100, b=1. up cost=5100=500, down cost=51=5. So demolish all 5 to get 0? Actually down cost demolish 5 to get 0, cost=5. That's min.
n=5,m=10,a=1,b=1. Both 5? Actually up=5, down=5. min=5. Could there be a combination cheaper? If we demolish 5 and build 10? That would end at 10 (since 5-5+10=10), cost =51+101=15, worse. So no.
n=7,m=5,a=2,b=3. r=2 (since 7%5=2). up cost=(5-2)2=6, down cost=23=6, min=6. Could there be a better N? For instance, demolish 7 to 0 cost=21; build to 5 cost=10; total 31. Worse. So fine.
Thus answer seems correct.
Now the problem might be from Codeforces Round #146? Actually it's likely problem "A. Commentary Boxes" or something. I recall that some people missed the simplicity and tried to brute force over k, but the constraints are high to prevent that. So the intended solution is to compute r = n % m and answer min(r*b, (m-r)*a). So we'll implement that.
Thus final solution: read integers, compute r = n % m, ans = min(r * b, (m - r) * a) if r != 0 else 0. But we can compute ans = min(r * b, (m - r) * a) because when r=0, both terms are 0 and ma? Actually if r=0, rb=0, (m-r)a = ma, so min yields 0. So we can just compute ans = min(r * b, (m - r) * a). But careful: (m - r) might be m when r=0, but min with 0 yields 0. So it's fine.
But note: (m - r) * a could be huge, but that's okay. In languages like C++ with 64-bit, it's fine as long as we use unsigned long long or long long. However, if r=0, (m - r) * a = ma which could be up to 1e12100 = 1e14, fits in 64-bit. So fine.
Alternatively, compute if n % m == 0: print 0 else compute ans = min( (n % m)*b, (m - n%m)*a ).
Thus code is trivial.
But wait: what about the possibility of negative numbers? Not allowed.
Thus we'll produce a solution.
Now let's consider if there is any scenario where building/demolishing multiple times might allow a cheaper combination if we consider that you can both build and demolish to reach a multiple that is not directly above or below, but the net change might be such that the cost of some operations is offset? For example, suppose a is very cheap and b is expensive. If we are above a multiple by a small amount, it's cheap to demolish that small amount? Actually if we are above, r is the remainder when n is above a multiple? Wait, n = qm + r, where 0 <= r < m. If r is small, down cost = rb is cheap if b is small, but up cost = (m-r)*a is larger if a is moderate. But if both a and b are cheap, maybe doing both could be cheaper than either pure add or pure subtract? Let's try to construct a counterexample.
Suppose n = 10, m = 6. Then r = 4. Up: need to add 2 to get 12, cost = 2a. Down: need to demolish 4 to get 6, cost = 4b. If a=5, b=6: up = 10, down = 24, so up is cheaper. Could there be a combination: maybe demolish 10 to 0 (cost 106=60) and then build 6 (cost 65=30) total 90 > up. So no.
But maybe if a is huge and b is huge? No.
What if we aim for N = 0? That's a multiple, down cost = nb. Could there be a cheaper way to get to some other multiple by both building and demolishing? For instance, n is large, and we are slightly below a multiple. Suppose n = 999, m = 1000, a = 100, b = 1. r = 999. Up: need to add 1 to get 1000, cost = 1100 = 100. Down: need to demolish 999 to get 0, cost = 9991 = 999. So up is cheaper. But what about demolishing 1 box to 998 and then building 2 boxes to 1000? That would cost 11 + 2100 = 1+200=201, worse than 100. Or demolishing 500 and building 500? cost 5001+500*100 = 500+50000=50500. So no.
What if we demolish all to 0 and then build to something else? That's always more expensive because you pay for demolishing all, then building many.
Thus pure strategy dominates.
Thus answer stands.
Now given the simplicity, the solution is just to compute r = n % m; print min(r * b, (m - r) * a). But need to be careful: The cost to build is a per box; cost to demolish is b per box. So if we decide to build up to next multiple, we build (m - r) boxes; if r=0, we build 0, not m. So our expression as above works with min, but it's safer to compute if r == 0: print 0; else ans = min(r*b, (m-r)*a). Because if r=0, (m-r)a would be ma, which is not a real cost needed (we don't need to build m boxes to be divisible, we already are divisible). But min with 0 gives 0, so it's fine. However, in programming, if we compute (m - r) * a, it might overflow? m up to 1e12, a up to 100, so product 1e14, within 64-bit. So no overflow.
Thus final code:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long long n, m, a, b;
cin >> n >> m >> a >> b;
long long r = n % m;
long long cost1 = r * b;
long long cost2 = (m - r) * a;
cout << min(cost1, cost2) << endl;
return 0;
}
In Python:
n, m, a, b = map(int, input().split())
r = n % m
print(min(r*b, (m-r)*a))
Wait: In Python, ints are arbitrary precision, so fine.
But note: The problem statement says "It is allowed that the final number of the boxes is equal to 0." So demolishing all to 0 is allowed, and that's included in cost1 when floor division yields 0? Actually if n < m, r = n, cost1 = n*b is cost to demolish all to 0 (since floor = 0). That's correct. So it's allowed to have final number 0.
But think about if m is 0? Constraints say m >= 1, so no.
Thus everything is consistent.
But wait, there is a nuance: The cost to build a box is a burles and demolish is b. In the problem they say "paying a burles and demolish a commentary box paying b burles". So per box. Yes.
Thus answer is as above.
Now some might argue that we could also consider not just the nearest multiple but also multiple of m that is exactly 0 when n < m, that's included. So all good.
Now why the examples: first matches, second matches, third matches.
Thus final solution is trivial.
But the problem may have been intentionally made simple, but perhaps there is a twist: what if the cost to build a box is cheaper than demolishing, but the remainder is large? It's still either building or demolishing to the nearest multiple. Because building to the next multiple might require many boxes, while demolishing to the previous multiple might require few boxes. So we take min.
Wait, but consider n=100, m=150, a=1, b=1000. r=100, up cost = (150-100)1 = 50, down cost = 1001000 = 100000. So up is cheaper. Could there be a better multiple further up? e.g., go to 300? That would cost 200*1 = 200 > 50. So no.
Consider n=100, m=150, a=1000, b=1. up cost = 501000 = 50000, down cost = 1001 = 100. So down cheaper. Could there be a better multiple further down? e.g., go to 0? cost = 100*1=100 (same as down cost, because floor(n/m)=0, r=100, down cost is exactly demolish all to 0, same as going to 0). So same.
Thus min of these two is optimal.
Thus answer is correct.
But there is a nuance: When n is divisible by m, then r=0, cost1=0, cost2=ma. The min is 0, so we output 0. However, is it possible that ma is less than 0? No. So fine.
Thus solution.
But I should also consider whether it's allowed to have final number negative? No.
Thus the solution is final.
But wait, there might be a scenario where the cost to build is very low and cost to demolish is also low, but the remainder is exactly half of m. Then both costs are comparable. Still min picks the lower.
So the solution is correct.
Now, I'll write the final answer with the code.
But before final, double-check with some random test:
n=1e12, m=1, a=100, b=1. m=1, any n is divisible by 1? Actually n % 1 = 0, so answer should be 0. Our formula: r = n%1 = 0; min(0, (1-0)*a) = 0. So output 0, correct.
n=1e12, m=1e12, a=100, b=1. n% m = 0 if n == m else? If n = 1e12, m=1e12, r=0 -> 0.
n=1e12-1, m=1e12, a=100, b=1. r = 1e12-1 (since n < m? Actually m=1e12, n=1e12-1 < m, so r = n = 1e12-1. cost1 = (1e12-1)1 = 1e12-1, cost2 = (1e12 - (1e12-1)) * 100 = 1100 = 100. min = 100. So building 1 box cost 100, correct.
n=1e12-1, m=1e12, a=1, b=100. cost1 = (1e12-1)100 huge, cost2 = 11 = 1, min=1, correct.
Thus works.
Thus solution complete.
Now, we must provide answer in the required format. The user asked: "Berland Football Cup ..." They likely expect code. So I'll output the solution code.
But the user is ChatGPT in a conversation; they might be expecting the explanation and the code. Since the instruction says "Solve the following competitive programming problem... Output the minimal amount..." But in the context, I need to provide the solution. Typically, I should provide the reasoning and the code. But as ChatGPT, I should output the solution as code? The prompt: "Solve the following competitive programming problem. ... Input: ... Output: ..." Possibly I need to give the answer? In many such interactions, the assistant provides the solution code. So I'll produce the code in Python and C++.
But the conversation: The user gave the problem statement, and expects the solution. Since this is a typical Codeforces problem, I'll provide the solution.
Thus final answer: Provide a brief explanation and the code.
I'll write:
We need to make n divisible by m. Let r = n % m. Then we can either remove r boxes (cost r*b) to get to the previous multiple, or add (m - r) boxes (cost (m - r)*a) to get to the next multiple (if r = 0, no change needed). The minimal cost is min(r * b, (m - r) * a).
Implementation: read integers, compute r, output min.
Then code:
Python:
def solve():
import sys
n, m, a, b = map(int, sys.stdin.read().split())
r = n % m
print(min(r * b, (m - r) * a))
if __name__ == "__main__":
solve()
C++:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long long n, m, a, b;
cin >> n >> m >> a >> b;
long long r = n % m;
long long cost1 = r * b;
long long cost2 = (m - r) * a;
cout << min(cost1, cost2) << endl;
return 0;
}
That should be accepted.
But double-check for the case r=0: cost2 = (m - 0)a = ma, but min with 0 yields 0. So fine.
Alternatively, we could handle r==0 separately to avoid multiplication, but not needed.
Thus final answer.
Now, is there any scenario where we might want to demolish to 0 even if floor(n/m)*m is not 0? Actually floor(n/m)*m is the largest multiple <= n. But what about 0? That's a multiple that is less than floor(n/m)m if floor(n/m) >= 1. For example, n=10, m=3, floor=3, multiple=9. Down cost = (10-9)b = 1b. But demolishing all to 0 costs 10b. That's more expensive. So we don't consider it because we only consider the nearest multiple below. But what if demolishing to 0 is cheaper than demolishing to the nearest multiple? That would require that b is negative? Not possible. Or maybe building to a multiple above is extremely cheap? No, demolishing to 0 is always more expensive than demolishing to the nearest multiple because you have to demolish more boxes. Unless the cost to demolish is negative (impossible). So it's never optimal to go to a lower multiple than the immediate previous multiple.
Thus we are done.
Now, I'll produce the final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n, m, a, b = map(int, data)
r = n % m
# cost to remove r boxes, or to add (m - r) boxes
cost = min(r * b, (m - r) * a)
print(cost)
if name == "main":
solve()