We are given two integers x and y (nonnegative up to 1e9). We need to make both zero. Two operations:
-
Pay a dollars: increase or decrease any of these integers by 1. So we can change x by +1 or -1, or y by +1 or -1. In effect, we can change the absolute value of x or y by 1 at cost a, but note we can also overshoot to negative values; that's allowed. So we can adjust each coordinate independently.
-
Pay b dollars: increase or decrease both integers by 1. That is, (x,y) -> (x+1, y+1) or (x-1, y-1). So this changes both coordinates simultaneously in the same direction.
Goal: reach (0,0) from (x,y). Find minimum total cost.
We have a,b positive.
We can think of this as we need to reduce |x| and |y| to zero. But operations can also increase them temporarily (like overshoot to negative), but likely optimal solutions will only reduce magnitudes? Usually in such problems, it's optimal to use operation 2 for the common part and operation 1 for the remainder. But we need to be careful because operation 2 changes both coordinates together; if we have to reduce both, we can apply operation 2 (decrease both) until one reaches zero, then use operation 1 for the other. However, operation 2 costs b each time, while two separate operation 1 would cost 2a. So if b < 2a, it's better to use operation 2 for the paired moves. But also, if b > 2a, it's better to always use operation 1. However, there is nuance: operation 2 moves both in same direction, but if one coordinate is positive and the other is negative? Actually x and y are given nonnegative (0 ≤ x,y). But we can go negative via operations, so maybe we could use operation 2 to bring both down to zero? But if one is larger than the other, after reducing both equally, the smaller becomes zero earlier; then we might need to increase it again? That might be wasteful. But maybe we can use operation 2 to reduce the larger one, while the smaller goes negative, then later bring it back up using operation 1? That could be beneficial if b is much cheaper than a? For instance, if b is very small, maybe it's cheaper to use operation 2 to reduce the sum, even if it makes one negative, and then correct the negative with operation 1. But operation 1 costs a per unit; making a coordinate negative and then bringing it back to zero costs extra. However, if b is extremely cheap (e.g., b=1, a=1000), perhaps it's cheaper to use operation 2 for all moves, even if it overshoots? Let's analyze.
We have starting point (x,y). We need to reach (0,0). Operations:
- Type 1: change one coordinate by ±1 at cost a.
- Type 2: change both coordinates by ±1 at cost b.
We want to minimize total cost.
This is reminiscent of many Codeforces problems. Typically, the solution is:
Let m = min(x,y). Then we can use operation 2 to reduce both by m (i.e., apply decrement both m times). After that, we have (x-m, y-m). Then use operation 1 for the remaining absolute values. Total cost = m * b + (x-m + y-m) * a = m * b + (x+y-2m) * a.
But this might not be optimal if b > 2a, because it's cheaper to use operation 1 for all moves. In that case, we would just use operation 1 for both coordinates: cost = (x + y) * a.
Also, there is possibility to use operation 2 even when one coordinate is zero? For example, we could use operation 2 to reduce the larger coordinate while the zero goes negative, then use operation 1 to bring the negative back to zero. This might be beneficial if b < a? But b is between 1 and 1e9, a also. If b < a, then operation 2 is cheaper than a single operation 1. But then using operation 2 to change both while one is zero: from (d,0), if we apply operation 2 to decrease both, we get (d-1, -1). To then fix the -1 to 0 costs a (increase y by 1). So net cost for reducing x by 1: b + a. That is more than using operation 1 alone (cost a) if b+a > a => b>0 always true? Actually b>0, so b+a > a. So that would be worse than just using operation 1 to decrease x. But if we use operation 2 to increase both? Starting from (d,0), we could increase both to (d+1,1) then later decrease? That seems even worse.
What about using operation 2 to decrease both when both positive but unequal? Suppose x > y. After reducing both by y using operation 2, we have (x-y, 0). Then we can either use operation 1 to reduce the remaining (x-y) at cost (x-y)*a. Alternatively, we could continue using operation 2: from (d,0) apply operation 2 to decrease both: (d-1,-1) cost b, then from (-1) we need to bring y to 0. Could use operation 2 again? From (-1) to 0 would require increasing, but if we apply operation 2 increase both: from (d-1,-1) increasing both gives (d,0) which is back; not helpful. To fix y, we could use operation 1 to increase y by 1: cost a. So net effect of reducing x by 1 from (d,0) using that combined method costs b + a. Compared to using operation 1 directly costs a. So only if b is negative? no. So not beneficial.
But could it be beneficial to use operation 2 in the opposite direction? For instance, if b is very small, maybe we use operation 2 many times to bring both to zero via some path that goes through negative coordinates and then back? Let's think more generally.
We have a graph on Z^2 with costs. Operation 1: move from (x,y) to (x±1,y) or (x,y±1) with cost a. Operation 2: move to (x±1,y±1) with cost b. We want minimal cost from (x,y) to (0,0). Since costs are uniform and operations are translation invariant, the minimal cost is some function f(x,y). We can think of it as a shortest path problem on infinite grid with positive edge weights. Since edges are undirected? Actually operations allow increase or decrease, so graph is undirected. So it's like we have two types of moves: Manhattan steps (axis-aligned) with weight a, and diagonal steps (both coordinates change in same direction) with weight b. But note diagonal steps here are only in the directions (1,1) and (-1,-1), i.e., only the main diagonal, not (1,-1) or (-1,1). So it's not the usual chess bishop moves; it's only along lines y = x + constant. So it's a kind of restricted moves.
We need to find minimum cost to go from (x,y) to (0,0). Since coordinates are nonnegative initially, but we can go negative, the graph is symmetric.
We can model this as linear programming? Possibly, we can think of the number of each operation.
Let u be number of type-2 operations that move towards decreasing both (i.e., (-1,-1) direction) and v be number of type-2 operations that move towards increasing both (i.e., (+1,+1)). But since we start at (x,y) and want to go to (0,0), net change in x is -x, net change in y is -y. So if we use d1 type-2 operations of the decreasing direction and e1 type-2 operations of the increasing direction, then net effect from type-2 is: Δx = -d1 + e1; Δy = -d1 + e1. So the net change in both coordinates from type-2 is (e1 - d1) in each coordinate. So let k = e1 - d1, which could be positive, negative, or zero. But d1 and e1 are nonnegative integers. Then from type-1 operations: we have moves that change x by ±1 and y by ±1 individually. Let a_x+ = number of type-1 increasing x, a_x- = decreasing x, similarly for y. Net Δx = (a_x+ - a_x-) + (e1 - d1) = -x. Net Δy = (a_y+ - a_y-) + (e1 - d1) = -y.
Total cost = a * (total type-1 moves) + b * (d1 + e1). Since type-1 moves are counted as sum of all individual adjustments.
We want to minimize cost.
We can think of it as we can decompose the movement into two components: the common part along line (1,1) direction, and the residual orthogonal part.
Let’s define the coordinates relative to the diagonal. Transform: Let s = x + y, d = x - y. But maybe not.
Better: Consider that type-2 moves affect both coordinates equally. So after using some net diagonal moves, we can align the coordinates to be equal, then use type-2 moves to reduce both to zero. However, we might also use type-2 moves to adjust the difference? Not directly, as type-2 does not change the difference x-y: (x+1)-(y+1)=x-y, (x-1)-(y-1)=x-y. So type-2 moves preserve the difference d = x - y. Indeed, if we apply (+1,+1) or (-1,-1), the difference stays same. So type-2 cannot change the difference between x and y. Only type-1 moves can change the difference, because they affect one coordinate at a time. Therefore, to get to (0,0), we must use type-1 moves to adjust the difference to zero eventually, because final difference is 0. Initially, difference is x-y (could be positive, negative, zero). So we need to eliminate this difference using type-1 moves. Moreover, after eliminating difference, both coordinates become equal (say to some value z), then we can reduce both to zero using either type-2 moves (if cheaper) or type-1 moves.
But careful: type-1 moves can also be used to reduce the common part. So a plausible strategy: Use type-1 moves to make x and y equal (i.e., reduce the difference to zero). Then use the cheaper per-unit method to reduce both from that common value to zero.
But could we intermix? Possibly we could use type-2 moves before making x=y, but since type-2 does not change the difference, any type-2 moves before equalizing would keep the same difference. So we could do type-2 moves first to reduce the minimum of x and y to some value, then use type-1 moves to adjust the difference. That is exactly what typical solution does: first use type-2 to reduce both by min(x,y) (the smaller) to make one zero and the other positive difference. Then use type-1 to bring the other to zero. But that's not the only order; you could also use type-1 first to equalize then type-2 to reduce both. Let's compute costs for these strategies.
Strategy A: Use only type-1: cost = (x+y)*a.
Strategy B: Use type-2 to reduce both as much as possible (i.e., by min(x,y) using decreasing direction), then type-1 for the remainder. That's cost = min(x,y)b + (x+y - 2min(x,y))*a = (x+y)a + min(x,y)(b - 2a). This is cheaper than A if b < 2a.
Strategy C: Use type-1 to equalize: to make x=y, we need to adjust the difference. Suppose x ≥ y. Then we can reduce x by (x-y) using type-1 (cost (x-y)a) to get (y, y). Then we have two equal numbers y. Then we need to reduce both to zero. We can either use type-2 to reduce both by y (cost yb) or use type-1 to reduce both separately (cost 2ya). So total cost = (x-y)a + min(2ya, yb). Since b vs 2a, we take the cheaper. So total = (x-y)a + ymin(2a, b). This simplifies to: if b ≤ 2a: cost = (x-y)a + yb; else: cost = (x-y)a + 2ya = (x+y)*a.
Now compare Strategy B: (x+y)a + min(x,y)(b - 2a) if b<2a, else (x+y)a (same as A). For b<2a, Strategy B = (x+y)a + min(b-2a) = (x+y)a - min(2a-b) = (x+y)a - min(2a-b). Strategy C for b<2a: (x-y)a + yb. Since we assumed x≥y, then min = y. Then C = (x-y)a + yb = xa - ya + yb = xa + y(b - a). While B = (x+y)a + y(b-2a) = xa + ya + yb - 2ya = xa + yb - ya = xa + y*(b - a). Indeed they are the same! So Strategy B and C give identical cost when x≥y? Let's check: B = min(x,y)*b + (x+y-2min)a. If x≥y, min=y, B = yb + (x+y-2y)a = yb + (x-y)*a. That's exactly C when b ≤ 2a? Actually C gave (x-y)a + ymin(2a,b). If b ≤ 2a, then C = (x-y)a + yb. So yes, B = C in that case. If b > 2a, then both B and C equal (x+y)a? Actually B with b>2a would be: B = yb + (x-y)*a? But that would be higher than (x+y)a? Let's compute: B = yb + (x-y)a. If b > 2a, then yb + (x-y)a > y2a + (x-y)*a = (2y + x - y)*a = (x+y)*a. So B would be larger than A. So the optimal strategy would not use B if b > 2a, because we can just use A. But wait, B is not necessarily the only way to use type-2; we could also use a mix that uses type-2 even if b>2a? That would be worse. So indeed the minimal cost would be min of A and B (and maybe other strategies). But as we derived, B is exactly using type-2 for the min part. And if b>2a, A is better. So the standard answer is:
Let ans = min( (x+y)*a, min(x,y)b + (x+y - 2min(x,y))*a ).
But is that always optimal? Let's test with examples.
Given sample: x=1, y=3, a=391, b=555. Compute min=1, (x+y)=4. Option1: 4391=1564. Option2: 1555 + (4-2)391 = 555 + 2391 = 555+782=1337. So min=1337. That matches sample.
Second sample: 0,0 => 0.
Now consider case where b < a? Suppose a=10, b=5. x=5, y=0. Standard formula: min(x,y)=0 => Option2: 0b + (5+0)a = 510=50. Option1: 510=50. So answer 50. But could we do better by using type-2? Since y=0, we could do type-2 decrement both: from (5,0) -> (4,-1) cost 5; then from (4,-1) we need to get to (0,0). We could then use type-1 to increase y from -1 to 0: cost 10, giving (4,0). Then use type-2 again? Let's see systematically: from (4,0) we can do similar: (4,0)->(3,-1) cost5 -> (3,0) cost10 (increase y) -> total so far: 5+10=15 for net reduction of x by 1? Actually initial x=5, after first cycle we have (4,0) after spending 5+10=15? Wait we started at (5,0). Step1: type-2 decrement: (4,-1) cost5. Step2: type-1 increment y: (4,0) cost10. Net: x decreased by 1, y back to 0, cost 15. That's worse than just type-1 decrement x (cost10). But maybe we can do multiple type-2 before fixing y? Suppose from (5,0) we do two type-2 decrements: (5,0)->(4,-1) cost5; then another type-2 decrement: can we apply to (4,-1)? Yes: (4,-1)->(3,-2) cost5 (since both -1). So after 2 steps, we have (3,-2) cost10. Then we need to bring y from -2 to 0. Could use two type-1 increments: each cost10, total 20, so final (3,0) cost30. So net reduction of x by 2 cost30 => 15 per unit, still worse. Could we use type-2 in the opposite direction? Not helpful.
What about using type-2 increment? Starting from (5,0), if we increase both: (6,1) cost5, then later decrease? That seems more steps.
Thus standard approach seems optimal.
But is there any scenario where using type-2 moves even when b > 2a could be beneficial if we also use type-1 moves to adjust the difference in a clever way? Suppose we have a large difference: x=100, y=0, a=1000, b=1500 (so b > 2a? Actually 2a=2000, b=1500 < 2000, so b < 2a, so beneficial to use type-2? Wait b=1500, a=1000, then 2a=2000, so b<2a, so using type-2 is beneficial. So the case b > 2a is when b > 2a. Then standard says use only type-1. But could there be a scenario where even though b > 2a, using type-2 might still be cheaper due to some clever combination? Let's try to construct a counterexample.
We need to get from (x,y) to (0,0). If b > 2a, then using one type-2 move costs more than two type-1 moves (since two type-1 moves can adjust both coordinates by 1 each? Actually two type-1 moves can decrease x by 1 and y by 1, costing 2a. So if b > 2a, it's cheaper to use two type-1 moves to reduce both by 1 rather than one type-2 move. However, type-2 also moves both by 1 but in a single operation. But since we can always achieve the same net effect with two type-1 moves at cost 2a, which is cheaper, we would never use a type-2 move if b > 2a, because any use of type-2 can be replaced by two type-1 moves (with possible adjustments) at lower cost? But careful: type-2 move changes both coordinates simultaneously, but maybe there is a scenario where using type-2 could reduce the number of type-1 moves needed because it also preserves something? But since we can always replace one type-2 move (say decrease both) with two type-1 moves (decrease x, decrease y) at total cost 2a. However, the order might matter if one coordinate becomes negative? But we can always apply the two type-1 moves in any order, and they won't affect each other. So indeed, if b > 2a, type-2 is strictly dominated by two type-1 moves for the purpose of decreasing both coordinates. But what about increasing both? Not needed if we are going to (0,0) from nonnegative start. We might need to increase if we overshoot? But overshooting might not be beneficial. So it seems that using type-2 when b > 2a is never beneficial.
Now consider the case where a < b < 2a. Then two type-1 moves cost 2a > b, so type-2 is cheaper for adjusting both coordinates. However, type-1 moves are still needed to adjust the difference. So the natural strategy: use type-2 to reduce both by the min, then type-1 for the remainder, is optimal. Could there be a scenario where we use more type-2 moves than min(x,y) (i.e., overshoot one coordinate to negative) and then use type-1 to bring it back, and this overall cost might be less? Let's analyze.
Suppose x >= y. Standard strategy uses y type-2 moves to bring (x,y) to (x-y, 0) and then uses (x-y) type-1 moves to bring x to 0. Total cost = y*b + (x-y)*a.
Alternative: Use y+k type-2 moves (k>0) to reduce both further: (x - (y+k), -(k)). Then we have x' = x-y-k, y' = -k (negative). To reach (0,0), we need to increase x' by (x-y-k)? Actually we need to get to (0,0): we need to increase y' to 0 (i.e., add k) and decrease x' to 0 (i.e., subtract x-y-k). But x' might be positive or zero? x' = x-y-k. Since x-y could be >=0. If k <= x-y, then x' >= 0. If k > x-y, then x' negative. Let's consider both.
Case: k <= x-y. Then after y+k type-2 decrements, we have (x-y-k, -k). Now we need to go to (0,0). We can use type-1 moves: increase y by k (cost k*a) to get (x-y-k, 0). Then decrease x by x-y-k (cost (x-y-k)a). So total cost = (y+k)b + ka + (x-y-k)a = yb + kb + (x-y)a. Since (x-y-k)a + ka = (x-y)a. So total = yb + (x-y)a + kb. This is greater than standard by kb. So worse.
Case: k > x-y. Then after y+k type-2 decrements, we have (x-y-k, -k) with x-y-k negative. Let d = x-y. Then coordinates: (d - k, -k). Both negative? Since d < k, d-k negative, and -k negative. To get to (0,0), we need to increase both coordinates. Options: use type-1 moves to increase x and y, or type-2 moves to increase both. Since we need to increase both, type-2 increasing moves (both +1) cost b. But we could also use type-1 moves. Let's compute optimal way from negative state to (0,0). This is symmetric to positive case but with signs flipped. Starting from (negative, negative) we can use type-2 increments to increase both. That's like the reverse. But note we can also use type-1 increments. The optimal from (u,v) where both <=0 is similar: using type-2 increments (if b < 2a) is beneficial. Let's compute total cost.
We have initial after y+k type-2 decrements: state S = (d - k, -k), where d = x-y >=0, k > d >=0.
We need to go to (0,0). Let u = -(d - k) = k - d (positive), v = k (positive). Actually S = ( - (k-d), -k). So we need to increase x by u = k-d, and increase y by v = k.
Now, from this state, the optimal cost to reach (0,0) would be: if we treat both coordinates as positive after flipping signs? Actually we need to increase both to zero. This is symmetric to starting from (u, v) positive, but we can only increase? Wait the operations allow both increase and decrease. To go from negative to zero, we can apply type-1 increase on a coordinate, or type-2 increase both. That is exactly analogous to going from positive numbers to zero by decreasing, just reversed. Since costs are symmetric (increase and decrease cost same), the minimal cost from (u,v) where u,v >=0 to (0,0) by decreasing moves is the same as from (-u,-v) to (0,0) by increasing moves. So the minimal cost from S to (0,0) equals the minimal cost from (u,v) to (0,0) using the same operations (since costs symmetric). And we already know the optimal cost from (u,v) is min( (u+v)*a, min(u,v)*b + (u+v-2min)*a ). However note that here u = k-d, v = k. So min(u,v) = min(k-d, k) = k-d (since d>=0). So the optimal cost from S is: either (u+v)*a = (2k-d)*a, or (k-d)*b + (u+v - 2(k-d))*a = (k-d)*b + (2k-d - 2k + 2d)*a = (k-d)*b + (d)a. Actually compute: u+v = (k-d)+k = 2k-d. Then u+v - 2min = (2k-d) - 2(k-d) = 2k-d - 2k + 2d = d. So cost = min(u,v)*b + (u+v-2min)*a = (k-d)b + da.
Thus the cost from S = min( (2k-d)*a, (k-d)b + da ). Since b is presumably less than 2a? In the scenario where we consider using extra type-2 moves, it's only interesting if b < 2a, else we would just use type-1. So assume b < 2a. Then which is smaller? Compare (2k-d)*a vs (k-d)b + da. Since b < 2a, (k-d)*b < (k-d)*2a. Then (k-d)b + da < (k-d)2a + da = (2k-2d + d)*a = (2k-d)*a. So the second option (using type-2) is cheaper than the first. So the optimal from S is (k-d)b + da. Note this is exactly the cost we would get if we treat the state (u,v) and use min(u,v) type-2 moves? That's consistent.
Thus total cost of the alternative strategy = cost of first phase (y+k type-2 decrements) + cost from S to (0,0) = (y+k)b + [ (k-d)b + da ]? Wait d = x-y. So total = yb + kb + (k-d)b + da = yb + (k + k - d)b + da = y*b + (2k - d)b + da.
But standard cost was yb + da (since d = x-y). So alternative cost exceeds by (2k - d)*b. Since k > d/2? Actually k > d, so 2k - d > d >0, so extra positive. So it's worse.
Thus using extra type-2 moves beyond min(x,y) is never beneficial.
What about using type-2 moves in the opposite direction (increase both) before decreasing? For example, from (x,y) we could increase both to make them more equal? But that would increase the sum, likely wasteful. But maybe if one is zero and the other is large, we could increase both to make them equal, then use type-2 to reduce both? Let's test: x=5, y=0, a=10, b=15 (b=15, 2a=20, so b<2a). Standard: min=0, cost=5a=50. Alternative: increase both by, say, 5: do type-2 increments 5 times: (10,5) cost 5b=75. Then we have (10,5). Then we could use type-2 decrements? But we need to go to (0,0). Could use min=5 type-2 decrements to get (5,0) cost 5b=75? Actually after increments we have (10,5). Then use 5 type-2 decrements to get (5,0) cost 5b=75. Then use type-1 to reduce x from 5 to 0 cost 5*a=50. Total = 75+75+50=200. Not good.
What about increase both by 1: (6,1) cost b, then use type-2 decrements maybe? Not helpful.
Thus standard seems optimal.
But wait, there is also possibility to use type-2 moves to adjust difference indirectly? Since type-2 does not change difference, no.
Thus the answer is straightforward: ans = min( (x+y)*a, min(x,y)b + (x+y - 2min(x,y))*a ).
But is there any scenario where b < a? Then maybe using type-2 moves for the difference part? Let's test a case where b < a. Example: a=100, b=1, x=100, y=1. Standard: min=1, cost = 11 + (101-2)100 = 1 + 99100 = 1 + 9900 = 9901. Option only type-1: 101100 = 10100. So standard better. But can we do better by using type-2 moves even for the difference? Since type-2 doesn't change difference, we need to adjust difference via type-1. However, perhaps we could use type-2 moves to reduce both to negative and then use type-1? But we already analyzed that overshoot adds extra cost.
Could we use type-2 moves to reduce both from (100,1) by, say, 50? Then we get (50, -49). Then we need to bring both to zero. The cost from (50, -49) to (0,0) would be something like: we can increase y by 49 (cost 49a=4900) to get (50,0), then reduce x by 50 (cost 50a=5000) total 9900 plus the 50 type-2 moves cost 50*1=50 gives 9950, worse than 9901. If we instead use type-2 moves to increase both from (50,-49) to adjust? Not better.
What about using type-2 moves to reduce both from (100,1) by 1? Already covered.
Thus standard stands.
However, there is nuance: In the standard formula, we assume we only use type-2 moves in the decreasing direction (towards zero). But could using type-2 moves in the increasing direction (away from zero) ever be beneficial? That would increase the sum, likely not.
Therefore, the answer is as given.
But wait, the problem statement says x and y are given as integers (0 ≤ x, y ≤ 10^9). So they are nonnegative. But operations allow decreasing/increasing any direction. So we may go through negative numbers, but as argued, not beneficial.
Thus the solution is simple.
But I recall a Codeforces problem "Collecting Coins" or something similar? Actually it's "Collecting Coins" from CF Round? Or "Two Types of Spells"? There's a known problem: "Two types of operations: pay a to change one, pay b to change both, find min to zero". The answer indeed is min((x+y)*a, min(x,y)*b + abs(x-y)a). But careful: (x+y - 2min) = |x-y|. Because if x>=y, then x+y-2y = x-y; if y>=x, then x+y-2x = y-x. So indeed it's min(x,y)*b + |x-y|*a. And (x+y)*a is another candidate.
But is there any scenario where we might use both increase and decrease type-2 moves that cancel each other, but overall reduce cost? That would be pointless because they'd add cost with no net effect.
Thus answer = min( (x+y) * a, min(x,y) * b + |x-y| * a ).
But wait, there is also the possibility to use type-2 moves to adjust the larger coordinate while the smaller is zero, but we already argued that results in extra cost b + a per unit vs a, so not beneficial unless b + a < a? impossible.
Thus final answer.
But the sample output is 1337 for first test and 0 for second. So matches.
Thus solution: For each test case, read x, y, a, b. Compute ans = min( (x+y)*a, min(x,y)*b + abs(x-y)*a ). Print ans.
But is there any nuance with integer overflow? x,y up to 1e9, a,b up to 1e9. (x+y)*a could be up to (2e9)*1e9 = 2e18, which fits in 64-bit (max ~9e18). So use 64-bit (long long in C++). In Python, integers arbitrary.
So code straightforward.
Edge Cases: x=0, y>0: then min=0, ans = min(ya, 0b + ya) = ya. That's correct: just use type-1 to reduce y to 0. Could we do type-2? If b < a? Could we reduce both? Starting from (0,y): we could increase x? Not needed. Could we decrease both? That would make x negative, then later fix, costing more. So not beneficial.
x=y>0: then ans = min(2xa, xb + 0a) = min(2xa, xb). So if b<2a, use b; else use 2a. That makes sense: either use x type-2 moves (cost xb) or 2x type-1 moves (cost 2xa). Could we mix? e.g., use some type-2 and some type-1? That would be cost something like kb + (2x-2k)a? But that's a convex combination; since linear, the minimum is at extreme: either k=0 or k=x. Because if b<2a, then decreasing k reduces cost? Actually cost = kb + (2x-2k)a = 2xa + k*(b-2a). Since b-2a is negative if b<2a, cost decreases with k, so maximize k, i.e., k=x gives min. If b>2a, cost increases with k, so k=0 min. So indeed extreme.
Thus answer is correct.
Now, the note in problem says: "In the first test case you can perform the following sequence of operations: first, second, first. This way you spend 391 + 555 + 391 = 1337 dollars." That matches using one type-2 (second) and two type-1 (first). That's exactly min(x,y)=1, |x-y|=2, cost = 1555 + 2391 = 1337. So they used that.
Thus the solution is trivial.
But wait, there might be another nuance: The operation 2 is "increase or decrease both integers by 1". That's exactly both +1 or both -1. So we can only change both in same direction. So indeed it's diagonal move. So our reasoning stands.
Double-check: Could there be a scenario where b is extremely cheap, like b=1, a=100, x=100, y=99. Standard: min=99, diff=1, cost = 991 + 1100 = 199. Option only type-1: 199100 = 19900. So standard best. Could we do better by using more type-2? Suppose we use 100 type-2 decrements: from (100,99) after 100 decrements we get (0,-1). Then we need to bring y from -1 to 0: type-1 increase cost 100. Total = 1001 + 100 = 200. That's slightly more than 199. So standard still better. What about 101 type-2 decrements? ( -1, -2) then need to increase both: using type-2 increments maybe? Let's compute minimal cost from (-1,-2) to (0,0). That's similar to (1,2) with decreasing moves? Actually from (-1,-2), we can use type-2 increments: one increment makes (0,-1) cost 1; second increment? Actually (0,-1) then need to increase y: type-1 cost 100, or type-2 increment would make (1,0) cost another 1 then need to decrease x? That's messy. Let's compute optimal from (u,v) negative: we can flip signs: let u' = -u, v' = -v, then we need to decrease from (u', v') to (0,0) using the same moves (since increasing is opposite of decreasing). So cost = min( (u'+v')*a, min(u',v')*b + |u'-v'|a ). For (-1,-2): u'=1, v'=2: cost = min(3100=300, min(1,2)*1 + |1-2|100 = 1+100=101). So from (-1,-2) to (0,0) optimal cost = 101. Then total = 1011 (for initial 101 type-2 decrements) + 101 = 202. Worse. So indeed extra type-2 not helpful.
Thus we have proven.
Now, we can implement.
But I've seen some Codeforces problem "Two types of spells" or "Collecting Coins" but there the answer is min(x,y)*min(b,2a) + |x-y|*a? Let's derive: If b < 2a, then min(x,y)*b + |x-y|*a is optimal. If b >= 2a, then (x+y)*a is optimal. That's same as min( (x+y)*a, min(x,y)*b + |x-y|*a ). So we can compute directly.
Alternatively, compute ans = min(x,y) * min(b, 2a) + (max(x,y)-min(x,y)) * a? Because if b <= 2a, then min(x,y)b + diffa; if b > 2a, then min(x,y)2a + diffa = (min2a + diffa) = (x+y)a? Actually min2a + diffa = (2min + (max-min))a = (max+min)a = (x+y)a. So yes, ans = min(x,y) * min(b, 2a) + |x-y| * a. That's a simpler expression: ans = |x-y| * a + min(x,y) * min(b, 2a). But careful: If b > 2a, then min(b,2a)=2a, so ans = |x-y|a + 2amin = a(|x-y| + 2min) = a*(x+y). So it matches. So we can compute as:
diff = abs(x - y)
same = min(x, y)
ans = diff * a + same * min(b, 2*a)
But is that always correct? Let's test with b = 2a exactly: then min(b,2a)=2a, ans = diffa + same2a = a*(diff+2same) = a*(x+y). Which equals (x+y)a. But also the other formula min((x+y)a, sameb + diffa) yields: sameb = same2a, plus diff*a = (x+y)*a, and min of that and (x+y)*a gives (x+y)a. So consistent. So we can use ans = diff * a + same * min(b, 2a). This is nice and avoids taking min of two large numbers? It's still okay.
But careful: There might be scenario where b is extremely large, say b = 1e9, a=1, then min(b,2a)=2, and ans = diff1 + same2 = diff + 2*same = x+y + same? Actually if x=5,y=0, diff=5, same=0, ans=5; but (x+y)a=5. So it's fine. For x=5,y=5, diff=0, same=5, ans=52=10; (x+y)*a=10, okay. So yes.
Thus ans = diff * a + same * min(b, 2*a). That is succinct.
But wait, there is a catch: In some problems, there is an additional nuance: If b < a, maybe it's cheaper to use two type-2 moves to adjust a single coordinate? But type-2 moves always affect both. Could we simulate a type-1 move using type-2 plus some other moves? For instance, to decrease x by 1 without affecting y, one could: increase both (type-2) then decrease both (type-2) and adjust? That would be net zero? Let's see: increase both: (x+1,y+1) cost b; then decrease x by 1 via type-1: (x,y+1) cost a; then decrease both? Not simple. Or using type-2 and type-1: to decrease x alone, we could decrease both (type-2) cost b, then increase y (type-1) cost a, net: x decreased by 1, y unchanged? Actually: start (x,y). Operation: type-2 decrement: (x-1,y-1) cost b. Then operation: type-1 increment y: (x-1, y) cost a. Net: x decreased by 1, y unchanged, total cost b + a. That's more than a (since b positive). So not beneficial unless b negative (impossible). Similarly, to decrease y alone: same. So no.
Thus answer stands.
Now we need to output integer.
Implementation details: read t, loop.
Potential pitfalls: Use 64-bit integers. In C++, use long long. In Python, int fine.
Now, I recall that there is a known Codeforces problem: "1368A - C+=" or something? Actually this problem is Codeforces Round #1462? Let me check: problem statement resembles "Collecting Coins" but not exactly. I think it's from Codeforces Round #1385? Or maybe "Two types of operations"? The sample mentions 1337, which is common. I think it's Codeforces problem 1557A? No.
Actually this is Codeforces problem 1550B? Not sure.
Anyway, we have the solution.
But wait, the problem says: "Pay a dollars and increase or decrease any of these integers by 1." It says "any of these integers", meaning you can change either x or y, but not both. So indeed type-1.
"Pay b dollars and increase or decrease both integers by 1." So type-2.
All good.
Thus the solution is straightforward.
I'll write the solution in Python.
Pseudo:
def solve():
import sys
input = sys.stdin.readline
t = int(input().strip())
for _ in range(t):
x, y = map(int, input().split())
a, b = map(int, input().split())
diff = abs(x-y)
same = min(x, y)
ans = diff * a + same * min(b, 2*a)
print(ans)
But double-check: For x=1, y=3, a=391, b=555: diff=2, same=1, min(b,2a)=min(555, 782)=555, ans = 2391 + 1555 = 782+555=1337. Good.
For x=0,y=0: diff=0, same=0, ans=0.
Edge: a and b up to 1e9, product may be up to 2e18, still within Python int.
Thus final answer.
But wait, there is a nuance: The expression diff * a + same * min(b, 2*a) might overflow 64-bit in C++ if we use int? Use long long. For safety: use long long.
Now, I've seen some solutions where they also consider using b for the diff if b < a? But diff can't be reduced by type-2 because type-2 doesn't change diff. So no.
But what if a is huge and b is small? Then min(b,2a)=b, ans = diffa + sameb. That's the usual.
Thus correct.
One more check: x=10^9, y=10^9, a=10^9, b=1. diff=0, same=10^9, min(b,2a)=1, ans=10^9. That's 10^9. But (x+y)*a = 2e9 * 1e9 = 2e18, so min is 10^9. Good.
x=10^9, y=0, a=1, b=10^9. diff=10^9, same=0, ans=10^9. min(b,2a)=2, but same=0 so ans=10^9. Option2: (x+y)*a=10^9. Option2 via min: min(x,y)b + diffa = 0 + 10^9 = 10^9. So same.
Thus fine.
Now, is there any scenario where the formula ans = diffa + same * min(b, 2a) might be wrong if b is less than a? Consider x=2, y=0, a=5, b=3. diff=2, same=0, ans=2*5=10. But could we do better using type-2? Standard says (x+y)*a=10, min(x,y)b+diffa=0+10=10. But maybe we can do: type-2 decrement: (1,-1) cost3. Then type-1 increment y: (1,0) cost5, total 8? Actually from (2,0): type-2 decrement: (1,-1) cost3. Then type-1 increment y: (1,0) cost5. Total 8, but we have (1,0) left. Then from (1,0) we need to go to (0,0). Could use type-1 decrement x: cost5, total 13. Or from (1,0) type-2 decrement: (0,-1) cost3, then type-1 increment y: (0,0) cost5, total additional 8, overall 3+5+3+5=16. So 8+? Actually first two moves: cost3+5=8, state (1,0). Then from (1,0) we can use type-1 decrement x: cost5 => total 13, final (0,0). That's 13, not 10. Or from (1,0) type-2 decrement then type-1 increment: 3+5=8 additional, total 16. So not better.
What about using type-2 increment? Not better.
Thus 10 is min.
Now, what about x=2, y=1, a=5, b=3. diff=1, same=1, ans = 15 + 1min(3,10)=5+3=8. Standard: min(x,y)b+diffa = 13+15=8. (x+y)a=35=15. So ans=8.
Now, consider if we could do: use type-2 decrement: (1,0) cost3. Then from (1,0) we need to go to (0,0). Could do type-1 decrement x: cost5, total 8. That's same. Could we do type-2 decrement again? (1,0)->(0,-1) cost3, then type-1 increment y cost5 => total 3+3+5=11. So 8 is best.
Thus ok.
Now, what about scenario where b < a, maybe we can use type-2 for diff? Not possible.
But could we use type-2 to reduce the larger while increasing the smaller? For instance, to adjust difference, we could do: increase both (type-2) to raise the smaller up to match larger, then use type-2 decrement both? That would be: from (x,y) with x>y, we could increase both by (x-y) using type-2? That would give (x + (x-y), y + (x-y)) = (2x-y, x)? Actually not; if we increase both by d where d = x-y? Then coordinates become (x+d, y+d) = (2x-y, 2y? wait y+d = y + (x-y) = x. So new state is (2x-y, x). That's not equal unless x=y. That didn't equalize. Actually to equalize, we could increase the smaller to match larger using type-1, but type-2 can't change difference. So no.
Thus answer is correct.
Now, a nuance: The problem says "increase or decrease any of these integers by 1" meaning you can choose to increase or decrease one of them. So yes.
Now, I've seen a Codeforces problem named "1360B? Not sure.
Given all that, the solution is straightforward.
But wait, is there any scenario where we might want to use type-2 moves to go negative and then use type-2 moves in opposite direction to get back? That would be like a loop: two type-2 moves, one decrement and one increment, net zero change but cost 2b. That's not helpful.
Thus answer.
Now, code in Python:
def solve():
import sys
input_data = sys.stdin.read().strip().split()
if not input_data:
return
it = iter(input_data)
t = int(next(it))
out = []
for _ in range(t):
x = int(next(it)); y = int(next(it))
a = int(next(it)); b = int(next(it))
diff = abs(x-y)
same = min(x,y)
cost = diff * a + same * min(b, 2*a)
out.append(str(cost))
sys.stdout.write("\n".join(out))
I'll include that.
But wait, the problem might have multiple test cases, up to 100, fine.
Now, I'd like to verify with some more test cases manually:
-
x=5,y=5,a=5,b=9 => 2a=10, min(b,2a)=9? Actually b=9 < 10, so min=9, ans = diff=0, same=5 => 0+5*9=45. (x+y)*a=50, min=45. So use b for both. But is that optimal? Using 5 type-2 moves cost 45, while 10 type-1 moves cost 50. So yes.
-
x=5,y=5,a=5,b=12 => 2a=10, min(b,2a)=10, ans = 5*10=50. (x+y)a=50, so equal. Could we do better by mixing? If we use k type-2 moves and rest type-1: cost = k12 + (10-2k)5 = 50 + k(12-10) = 50 + 2k, so min at k=0 =>50. So yes.
-
x=1000000000,y=0,a=1,b=1000000000 => diff=1e9, same=0, ans=1e9. Good.
-
x=1000000000,y=1000000000,a=1000000000,b=1 => diff=0, same=1e9, min(b,2a)=1, ans=1e9. (x+y)a=2e91e9=2e18, so ans=1e9. That would be huge difference! Actually 1e9 cost vs 2e18, so definitely using b is cheaper.
Now, but is it always possible to use b to reduce both? Yes, we can apply type-2 decrement exactly same times as min(x,y). That yields (x-min, y-min). Since both nonnegative, after applying min times, one becomes zero, the other positive (or both zero). That's valid. So we can always achieve that.
Thus solution is correct.
Now, the output for each test case is a single integer.
Thus final answer.
I will now produce final output with explanation and code.
But wait, there might be a twist: In operation 1, "increase or decrease any of these integers by 1" means you can change either x or y, not both. That's what we used.
Thus final.
Now, I'll craft the response: include solution explanation and code.
Given the instructions: The assistant should provide a helpful answer. I'll output the code in Python.
But the user hasn't explicitly asked for code; they gave a problem statement. Typically they want solution. I'll provide explanation and code.
Thus final. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
x = int(next(it))
y = int(next(it))
a = int(next(it))
b = int(next(it))
diff = abs(x - y) # |x - y|
same = min(x, y) # min(x, y)
# cost = diff * a + same * min(b, 2*a)
cost = diff * a + same * (b if b < 2 * a else 2 * a)
out_lines.append(str(cost))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()