We are given two points a and b (integers from 1 to 1000, a ≠ b). Two friends start at integer coordinates a and b. They can move left or right along the integer line, each move steps exactly one unit. Each friend accumulates tiredness: the first move costs 1, second move costs 2, third costs 3, etc. So the cost for each friend for a sequence of moves is the sum of 1+2+...+k = k(k+1)/2, where k is the number of moves made by that friend. They can move arbitrarily (including moving back and forth) but the total tiredness is sum over both friends of their individual tiredness sums. They want to meet at some integer point (not necessarily the starting points? They need to end up at same integer point after each friend has performed some number of moves, possibly zero moves). Determine the minimum total tiredness.
Observation: They can move arbitrarily, but wasted moves (like moving away from the meeting point) would increase tiredness unnecessarily. Since they want to minimize total cost, they should only move directly toward the meeting point without extra detours because any extra moves would add extra costs. However, note that the cost depends on the number of moves, not the distance traveled. So it's like they each have a per-move cost that increases each move. They need to end at a common point. They can choose any meeting point x (integer). The cost for friend at a to go to x is the cost of making d = |x - a| moves in the direction towards x (if x > a, then right moves; if x < a, left moves). But they could also make additional moves and then come back, but that would only increase cost, so optimal is to take exactly d moves in a straight line. So cost for friend i: if they need to travel distance d (positive integer) to reach x, then the minimal cost for that distance is the sum of 1..d = d(d+1)/2? But careful: They could choose to make some moves to the left and then to the right and still end up at x, but that would increase total number of moves beyond d. Since the costs are increasing, it's always worse to have extra moves. However, is there any scenario where making extra moves could reduce total cost for both? Possibly if the meeting point is not directly between them, but you could have one friend go a bit more and the other less, etc. But we can choose any meeting point. So we need to choose x and each friend's move count is exactly |x - a| and |x - b| (assuming they don't waste moves). That yields total cost f(x) = d1(d1+1)/2 + d2(d2+1)/2, where d1 = |x - a|, d2 = |x - b|, and x integer.
But is it always optimal to have them go directly? Could there be a situation where one friend makes extra moves to adjust the meeting point to a more favorable location? Actually, the meeting point is determined by their final positions; if one friend makes extra moves and ends at some point, the other also ends at that same point. So the total cost is determined by the sequences of moves each makes. If you consider that they might not both travel directly, the effective distances each covers (net displacement) is |x - a| and |x - b|, but the number of moves could be larger than the net distance if there are back-and-forth moves. Since costs increase with number of moves, any extra moves beyond the net displacement will increase cost. So for a given meeting point x, the minimal cost for each friend is indeed the sum of first d moves, where d is the net distance. So the problem reduces to: find integer x that minimizes f(x) = d1(d1+1)/2 + d2(d2+1)/2, with d1 = |x-a|, d2 = |x-b|, a,b given, a≠b.
So we need to compute min_{x∈Z} [ |x-a|(|x-a|+1)/2 + |x-b|(|x-b|+1)/2 ].
We can treat the function f(x) piecewise quadratic. Since a and b are integers, we can consider x on integer line. Because the function is convex? Let's check: For a single term, g(d) = d(d+1)/2 = (d^2 + d)/2, which is convex (since quadratic with positive coefficient). The sum of convex functions is convex. However, absolute values create piecewise linear distances, but g(|x-a|) is convex as well because composition of convex non-decreasing function with convex function? Actually |x-a| is convex, and g is convex non-decreasing for nonnegative arguments, so g(|x-a|) is convex. So f(x) is convex. Therefore the minimum occurs at a point where derivative changes sign, possibly at a point between a and b? Since convex on ℝ, the minimum of a convex function over integers can be found by evaluating at integer points near the global minimizer over reals.
We can try to find x that minimizes f(x). Write f(x) = 1/2 [ (x-a)^2 + |x-a| + (x-b)^2 + |x-b| ]. Because d^2 = (x-a)^2 when we square distance, but careful: d = |x-a|, so d^2 = (x-a)^2, and d = |x-a|. So f(x) = 1/2[ (x-a)^2 + |x-a| + (x-b)^2 + |x-b| ].
Alternatively, we can think of splitting into cases based on relative ordering of x with a and b. Since a and b are distinct, assume w.l.o.g. a < b. Then for x, three intervals: x ≤ a, a ≤ x ≤ b, x ≥ b.
Case 1: x ≤ a (so x ≤ a < b): then |x-a| = a-x, |x-b| = b-x. So f(x) = (a-x)(a-x+1)/2 + (b-x)(b-x+1)/2. Expand? Compute as function of x. Let’s define u = a-x, v = b-x, with u = a-x >= 0, v = b-x >= 0, and v = b-x = (b-a)+(a-x) = (b-a)+u. So f = u(u+1)/2 + (u + (b-a))(u + (b-a) + 1)/2. That is increasing in u? Since x decreasing increases u. For x ≤ a, as x increases (to the right), u decreases, so f decreases? Let's analyze derivative. Since convex, we can find the optimum might be in the middle region.
Case 2: a ≤ x ≤ b: then |x-a| = x-a, |x-b| = b-x. So f(x) = (x-a)(x-a+1)/2 + (b-x)(b-x+1)/2.
Case 3: x ≥ b: then |x-a| = x-a, |x-b| = x-b. So f(x) = (x-a)(x-a+1)/2 + (x-b)(x-b+1)/2.
Given convex, the minimum likely occurs in the interval [a, b] (the convex combination). So we can focus on case 2. Let’s analyze f(x) for integer x between a and b inclusive.
Let d1 = x - a, d2 = b - x. d1,d2 are nonnegative integers. Then f(x) = d1(d1+1)/2 + d2(d2+1)/2 = (d1^2 + d1 + d2^2 + d2)/2.
We want to minimize this over integer x in [a,b]. Since d1 + d2 = b - a = L (the distance between a and b). Because b - a = (x-a)+(b-x) = d1 + d2. So L = b - a > 0. So we have d1 + d2 = L fixed. So we want to minimize g(d1) = d1(d1+1)/2 + (L - d1)(L - d1 +1)/2 for integer d1 from 0 to L.
Simplify g(d1):
g = (d1^2 + d1 + (L-d1)^2 + (L-d1)) /2 = (d1^2 + d1 + L^2 -2L d1 + d1^2 + L - d1) /2? Let's compute carefully:
(L-d1)^2 = L^2 - 2L d1 + d1^2.
(L-d1) = L - d1.
So sum: d1^2 + d1 + (L^2 - 2L d1 + d1^2) + (L - d1) = 2 d1^2 + (d1 - d1) cancels? Actually d1 + (L - d1) = L. So total = 2 d1^2 + L^2 - 2L d1 + L.
Thus g = (2 d1^2 - 2L d1 + L^2 + L)/2 = d1^2 - L d1 + (L^2 + L)/2.
So g(d1) = d1^2 - L d1 + (L^2 + L)/2.
This is a quadratic in d1, convex (coefficient 1 > 0). Its minimum over real numbers occurs at d1 = L/2. So the integer d1 that minimizes is the integer(s) closest to L/2. Since d1 integer between 0 and L inclusive.
Thus minimal total tiredness for meeting somewhere between a and b is achieved when d1 is floor(L/2) or ceil(L/2). Then g_min = (d1^2 - L d1) + (L^2+L)/2.
Simplify g_min for integer d1 near L/2.
Let’s compute g_min in terms of L.
Let d1 = floor(L/2). Then d2 = L - d1 = ceil(L/2). Actually if L is even, L=2k, then optimal d1 = k, d2 = k. Then g = k^2 - 2kk? Wait compute: d1 = k, L = 2k. Then d1^2 - L d1 = k^2 - 2kk = k^2 - 2k^2 = -k^2. Then (L^2 + L)/2 = (4k^2+2k)/2 = 2k^2 + k. So g = -k^2 + 2k^2 + k = k^2 + k = k(k+1). So total f = k(k+1). But f is total tiredness: d1(d1+1)/2 + d2(d2+1)/2 = k(k+1)/2 + k(k+1)/2 = k(k+1). Yes.
If L is odd, L=2k+1. Then optimal d1 = k or k+1. Let’s take d1 = k, d2 = L - k = (2k+1)-k = k+1. Then compute f = k(k+1)/2 + (k+1)(k+2)/2. Compute sum: [k(k+1) + (k+1)(k+2)]/2 = (k+1)[k + (k+2)]/2 = (k+1)(2k+2)/2 = (k+1)*2(k+1)/2 = (k+1)^2. So total f = (k+1)^2. Check d1 = k+1 gives same: (k+1)(k+2)/2 + k(k+1)/2 = same. So total f = (k+1)^2.
Thus minimal total tiredness for meeting in between a and b (i.e., x integer between a and b inclusive) is:
If L = b - a (positive) even: L = 2k → f_min = k(k+1).
If L odd: L = 2k+1 → f_min = (k+1)^2.
But we must also consider the possibility that meeting point outside [a,b] could yield lower cost. Since f is convex and we suspect minimum inside, but we should confirm. For x ≤ a, f(x) = u(u+1)/2 + (u+L)(u+L+1)/2, where u = a - x ≥ 0. Then f(x) as function of u (u integer ≥ 0) = (u^2+u)/2 + ((u+L)^2+(u+L))/2 = (u^2+u + (u+L)^2+(u+L))/2 = (u^2+u + u^2+2uL+L^2 + u+L)/2 = (2u^2 + 2uL + L^2 + 2u + L)/2 = u^2 + uL + (L^2+L)/2 + u? Wait: (2u^2 + 2uL + L^2 + 2u + L)/2 = u^2 + uL + (L^2)/2 + u + L/2? Let's recalc:
2u^2+2uL+L^2+2u+L all over 2 = u^2 + uL + (L^2)/2 + u + L/2.
So f(x) = u^2 + uL + u + (L^2+L)/2? Actually (L^2)/2 + L/2 = (L^2+L)/2. So indeed f = u^2 + uL + u + (L^2+L)/2 = u(u + L + 1) + (L^2+L)/2.
Since u ≥ 0, this is increasing in u (as derivative >0 for u>0). Check: For u=0, x=a, then f = (L^2+L)/2? But wait at x=a, that's when one friend stays at a, the other travels distance L. That yields f = 0 + L(L+1)/2 = L(L+1)/2. Indeed (L^2+L)/2 = L(L+1)/2. So at u=0, f = L(L+1)/2. That is larger than our candidate minimum for inside? Let's compare: L(L+1)/2 vs our minimal inside f_min.
Case L=2k: L(L+1)/2 = (2k)(2k+1)/2 = k(2k+1). Inside min = k(k+1). Which is smaller? k(k+1) vs k(2k+1). Since k+1 ≤ 2k+1 for k>=1, clearly inside min is smaller. For k=1 (L=2): inside min = 12=2; endpoint a: 23/2=3; indeed larger.
L odd: L=2k+1: L(L+1)/2 = (2k+1)(2k+2)/2 = (2k+1)(k+1) = (k+1)(2k+1). Inside min = (k+1)^2. Compare: (k+1)^2 vs (k+1)(2k+1): (k+1)^2 ≤ (k+1)(2k+1) for k>=0. So indeed endpoint is larger.
Thus moving x left of a gives f larger than at x=a (since u>0). Similarly for x≥b, it's symmetric and gives f at x=b = L(L+1)/2 as well, and then increasing as we go further right. So the minimum indeed occurs within [a,b] (including endpoints? Actually endpoints give larger than inside optimum; but if L=1 (adjacent points), then a and b are distance 1. Then possible meeting points: x can be a or b or somewhere in between? There is no integer between a and b if they differ by 1. So the only integer points are a and b. Then we must consider those endpoints. In that case L=1 (odd). According to our formula for inside: L=2k+1 with k=0 => inside min would be (k+1)^2=1. But that's for x in between? But if L=1, there is no integer strictly between a and b. However, we could possibly meet at a or b. At x=a: cost = 0 + 12/2? Actually distance from a to a =0, cost0; b to a distance=1 => cost=1. So total=1. Similarly at x=b: total=1. So indeed min total =1, which matches (k+1)^2 with k=0 =>1. So our inside formula works if we consider the endpoints as inside? In the interval [a,b] inclusive, the endpoints are allowed. In our derivation we considered d1+d2=L with d1 = x-a, d2 = b-x, which is valid for x in [a,b] inclusive. At x=a, d1=0, d2=L; at x=b, d1=L, d2=0. So those are included. And we minimized over d1 from 0 to L. So our formula includes endpoints. Our derivation of optimum d1 = L/2 (real) gives integer near L/2. If L=1, L/2=0.5, nearest integers are 0 and 1. So we consider both endpoints. Then min g(0) = g(1) = (0^2-10+(1+1)/2?) Actually compute: L=1, then g(d1) formula: g = d1^2 - 1*d1 + (1+1)/2 = d1^2 - d1 + 1. At d1=0: g=1; at d1=1: g=1. So min=1. So consistent. So the minimum over integer x is given by the formula: f_min = ?
We derived that minimal total tiredness is:
Let L = |a - b|. (Since we can assume a < b, L = b - a.) Then minimal total tiredness = ?
Case L even: L = 2k → f_min = k(k+1).
Case L odd: L = 2k+1 → f_min = (k+1)^2.
But check examples:
Example 1: a=3, b=4 → L=1 (odd, k=0) → f_min = (0+1)^2 = 1. matches.
Example 2: a=101, b=99 → we need to compute L = |101-99| = 2 → even, k=1 → f_min = 1*2 = 2. matches.
Example 3: a=5, b=10 → L=5 → odd, k=2 (since 5=2*2+1) → f_min = (2+1)^2 = 9. matches.
Thus solution seems to simply compute L = |a-b|, then if L even: L/2 * (L/2 + 1) else ((L+1)/2)^2.
Wait, check formula for odd: L=5 → (L+1)/2 = 3, squared =9. So f_min = (L/2 rounded up)^2? Actually for odd L, (L+1)/2 = ceil(L/2). So f_min = (ceil(L/2))^2. For even L, L/2 = k, f_min = k*(k+1) = (L/2)*((L/2)+1).
Thus answer can be computed directly.
But wait, is there any nuance? a and b are up to 1000, but this seems trivial. The problem likely expects this reasoning. However, we should double-check if there is any possibility of reducing total tiredness by having them meet at a point outside the interval and one friend travels more but with lower per-step cost? Because the cost per step increases, so traveling longer distance incurs much higher cost. Could there be a scenario where moving both friends further away reduces total cost? For instance, if both move towards each other and overshoot? That would increase distance traveled for each. No.
But perhaps there is a possibility where they don't travel directly to the meeting point but rather one friend does extra moves to "reset" the cost counter? Because the tiredness increases with each move: the first move costs 1, second 2, etc. But if a friend makes moves in one direction and then back, the cost sequence continues to increase, so that would not reset. There's no way to lower the incremental cost; it always increases. So it's always best to minimize the number of moves for each.
Thus the solution is indeed simple.
But wait: The problem statement says "Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the following rules: the first move increases the tiredness by 1, the second move increases the tiredness by 2, the third — by 3 and so on." So the tiredness is cumulative sum of move costs. So indeed if a friend moves k times, total tiredness = 1+2+...+k = k(k+1)/2. There is no possibility to "reset" the counter. So we are correct.
Thus the answer can be computed easily.
But let's also consider if meeting point could be non-integer? The problem says "meet in a integer point." So meeting point must be integer.
Thus solution: read a, b, compute d = abs(a - b). Then if d % 2 == 0: let half = d//2; answer = half * (half + 1). else: half = (d+1)//2; answer = half * half.
Double-check with example: d=2: half=1; ans=12=2. d=1: half=1; ans=11=1. d=3: (3+1)//2=2; ans=4. Let's test manually: a=0,b=3? Not in range but just test: possibilities: x=1? d1=1,d2=2 → cost=1+3=4? Actually 12/2=1, 23/2=3 → total=4; x=2: d1=2,d2=1 → 3+1=4; x=0: cost=0+6=6; x=3:6; So min=4. So correct.
Thus solution is O(1).
Now, we must write solution code accordingly.
But hold on: Could there be a scenario where it's beneficial for one friend to make extra moves to even out the distances? Because the cost function is convex per distance, but we already considered the optimal split of L between d1 and d2. That is essentially balancing the distances each travels to minimize sum of quadratic functions. Since the cost per unit distance increases with distance, we want each to travel as equal distances as possible. Indeed, the optimal split is to have d1 ≈ d2. So meeting point near the middle yields minimum.
Thus answer as above.
Hence solution: compute diff = abs(a - b). Let k = diff // 2. If diff even: ans = k * (k+1). Else: ans = (k+1) * (k+1). Because diff odd: diff=2k+1, then (k+1)^2.
Implementation straightforward.
Edge cases: diff=0? But a ≠ b per input guarantee. So diff≥1.
Thus code.
But wait, is there any tricky case where meeting at a point outside yields less? Let's test with a=1,b=4: diff=3, ans=4 as computed. If x=5 (outside), then d1=4, d2=1 → cost=10+1=11? Actually 45/2=10, 12/2=1 → total=11 >4. If x=0: d1=1,d2=4 → total=1+10=11. So no.
So solution is correct.
Thus final answer.
Now, think about whether the problem might have intended something else: "tiredness" increases per move, but perhaps they can move simultaneously? But total tiredness is sum of tiredness of each friend, independent. So still same.
Thus answer is simple.
Now, we produce solution explanation: The minimal total tiredness = t, where t = (d//2) * (d//2 + 1) if d even, else ((d+1)//2)^2.
I'll output the answer.
But check sample: Input 101, 99: diff=2 even -> half=1 -> 1*2=2 correct.
Thus answer.
But wait, sample 3: 5,10 diff=5 odd -> (5+1)//2=3 -> 9 correct.
Thus final.
But before finalizing, I'd like to think about a more rigorous approach: The function f(x) = |x-a|(|x-a|+1)/2 + |x-b|(|x-b|+1)/2. This is convex, so the minimal integer x is floor or ceil of the real minimizer. Set derivative for x in (a,b): derivative of first term: (2(x-a)+1)/2 * sign? Actually derivative of (x-a)^2/2 + (x-a)/2 = (x-a) + 0.5? But with absolute? Within (a,b), we have x-a positive, b-x positive, so f(x) = (x-a)^2/2 + (x-a)/2 + (b-x)^2/2 + (b-x)/2. Derivative: (x-a)+0.5 - (b-x)-0.5 = (x-a)-(b-x)=2x - (a+b). Set to zero => x = (a+b)/2. So the real minimizer is the midpoint. The integer minimizer is floor or ceil of midpoint. That yields the same as our analysis. And the minimal value can be expressed in terms of L.
When L is even, midpoint is integer, so x = (a+b)/2. Then distances: d1 = d2 = L/2, cost = 2 * ( (L/2)(L/2+1)/2 ) = (L/2)(L/2+1). For L odd, midpoint is half-integer; the two nearest integer points are at distances floor(L/2) and ceil(L/2) leading to cost = (floor(L/2))(floor(L/2)+1)/2 + (ceil(L/2))(ceil(L/2)+1)/2 = as derived.
Thus final.
So answer.
Given that input constraints are small, any solution would work, but O(1) is fine.
Thus I'll write solution in Python.
But also mention that one could iterate over x from a to b, but direct formula is better.
Now produce final answer.
Implementation details: read a, b as ints, compute diff = abs(a - b), if diff % 2 == 0: half = diff // 2; ans = half * (half + 1); else: half = (diff + 1) // 2; ans = half * half; print(ans).
Check: diff=1 -> half=1, ans=1; diff=2 -> half=1, ans=2; diff=3 -> half=2, ans=4; diff=4 -> half=2, ans=6? Wait diff=4 even: half=2 => 2*3=6. Let's test manually: a=0,b=4 -> possible meeting points: x=2: distances 2 and 2 => costs 3+3=6; x=1: 1 and 3 => 1+6=7; x=3: 3+1=7; x=0:0+10=10; x=4:10+0=10; min=6. So correct.
Thus solution.
Now, note: The problem is from Codeforces? Probably. Yes, Codeforces problem 1278A? No, maybe it's 1260A? Actually I recall a problem "Heating" but that's different. This problem is "Two Friends" maybe? I think it's Codeforces Round #... Doesn't matter.
Thus final answer.
But let's be thorough: Could there be any other nuance? The friends start at integer coordinates but they can move "by one along the line in any direction unlimited number of times". That implies each move changes coordinate by ±1. That's exactly step movement. So our model is correct.
Thus answer.
Now, we can provide a solution.
But wait, there is one more nuance: The tiredness increases with each move regardless of direction, and the first move costs 1, second 2, etc. So if a friend makes 0 moves, tiredness = 0. So that's accounted.
Now, also note that the meeting point could be one of the starting points, which yields cost for the other friend equal to L(L+1)/2. That's larger than the min unless L=0 or maybe L=1? For L=1, L(L+1)/2 = 1, which equals min? For L=1, min is also 1. So it's the same. But for L>1, min is less.
Thus final.
Thus produce final answer.
Edge: a and b up to 1000, diff up to 999. So answer fits within int range.
Now, we might also consider possibility of negative coordinates? Input says a,b are between 1 and 1000 inclusive, so positive, but that doesn't matter.
Thus implement.
But the problem might be more interesting if the cost per move is triangular numbers. But indeed it is.
Alright.
Given the simplicity, we might also produce a proof explanation: Since each friend's tiredness after k moves is k(k+1)/2, and they must travel net distances d1 and d2 to meet, minimal total tiredness is achieved by making no extra moves, so total = d1(d1+1)/2 + d2(d2+1)/2. The sum d1+d2 = |a-b| is fixed regardless of meeting point? Actually if meeting point is not between them, the sum is greater than |a-b|? Let's check: If meeting point is outside, say x < a < b, then d1 = a - x, d2 = b - x, sum = b + a - 2x > b - a = L, since x < a. So sum of distances > L. So indeed the sum of distances is minimized when the meeting point lies between them, giving sum = L. Since costs increase with distance, the best should be within the interval, because moving outside increases both distances. So the optimum lies in [a,b] (inclusive). Then we minimize over d1+d2 = L the convex function. So the answer is as given.
Thus done.
Now code.
We'll produce a python script:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
a = int(data[0])
b = int(data[1])
d = abs(a - b)
k = d // 2
if d % 2 == 0:
ans = k * (k + 1)
else:
ans = (k + 1) * (k + 1)
print(ans)
if name == "main":
solve()
That's it.
But wait: In sample input, they are given in separate lines. So reading like this works.
Thus final answer.
But I'd like to double-check one more: a=1, b=1000: d=999. odd: k=499? Actually 999//2=499, k+1=500, ans=500^2=250000. Compute maybe manually? Not needed.
Thus solution.
Therefore we can produce answer.
But could there be an alternative interpretation: The tiredness increases per move but maybe the tiredness is per friend and resets after meeting? No.
Thus final.
Given that this is a simple problem, I'll just output final solution.
But as ChatGPT, we need to output a final answer in the required format? The user asks: "Determine the minimum total tiredness they should gain, if they meet in the same point." So we must output number.
Thus I'll produce final answer as the solution.
Now, I'll craft final message accordingly.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
a = int(data[0])
b = int(data[1])
d = abs(a - b)
k = d // 2
if d % 2 == 0:
ans = k * (k + 1)
else:
ans = (k + 1) * (k + 1)
print(ans)
if name == "main":
solve()