Polycarp has guessed three positive integers a, b and c. He keeps these numbers in secret, but he writes down four numbers on a board in arbitrary order — their pairwise sums (three numbers) and sum of all three numbers (one number). So, there are four numbers on a board in random order: a+b, a+c, b+c and a+b+c.
You have to guess three numbers a, b and c using given numbers. Print three guessed integers in any order.
Pay attention that some given numbers a, b and c can be equal (it is also possible that a=b=c).
Input
The only line of the input contains four positive integers x_1, x_2, x_3, x_4 (2 ≤ x_i ≤ 10^9) — numbers written on a board in random order. It is guaranteed that the answer exists for the given number x_1, x_2, x_3, x_4.
Output
Print such positive integers a, b and c that four numbers written on a board are values a+b, a+c, b+c and a+b+c written in some order. Print a, b and c in any order. If there are several answers, you can print any. It is guaranteed that the answer exists.
Examples
Input
3 6 5 4
Output
2 1 3
Input
40 40 40 60
Output
20 20 20
Input
201 101 101 200
Output
1 100 100
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve the problem: given four numbers (positive integers) which are, in some order: s1 = a+b, s2 = a+c, s3 = b+c, and s4 = a+b+c, where a, b, c are positive integers. We need to recover a, b, c (any order). Input: four numbers x1..x4 in random order, guarantee that a solution exists.
We need to output a, b, c (positive integers). Since a,b,c > 0 (positive integers) because sums are >= 2, but they might be 1? Input constraints: each xi ≥ 2, but a,b,c could be 1 (since e.g., a=1,b=1,c=1 gives sums:2,2,2,3). So yes.
Approach: Without loss of generality, we know that the largest of the four numbers is a+b+c, because each pairwise sum is at most (a+b+c) since c>=1 => a+b <= a+b+c, etc. Actually, for positive integers, a+b < a+b+c, because c>0, similarly others. So the maximum among the four numbers must be a+b+c. However, what if there are ties? Could a+b equal a+b+c? That would require c=0, but c is positive, so no. So max is strictly greater than the other three. But note that if a=b=c=1, sums are 2,2,2,3, so max=3 unique. In cases like 2,2,2,3, max is 3 unique. Could there be ties among the pairwise sums? Yes, e.g., a=b, then a+b and a+c and b+c? Actually a+b = 2a, a+c = a+c, b+c = a+c if a=b, so two equal but not all three maybe. Also all three pairwise sums could be equal if a=b=c, then a+b = 2a, a+c = 2a, b+c = 2a, all equal; then the max (a+b+c=3a) is larger than 2a for a>0. So the max is always unique? Possibly not unique if a+b+c equals one of the pairwise sums? That would require c=0, not possible. So yes, max is unique. However, there might be cases where two numbers equal max? Since numbers are given, if there are duplicate values, the maximum value might appear multiple times if the input contains duplicates of that value? But a+b+c is strictly larger than any pairwise sum, so if the max value occurs more than once, that would be impossible because that would require two numbers both equal to a+b+c, but we only have one a+b+c. However the input could contain duplicate numbers from the pairwise sums: e.g., a=b, then a+b = 2a, a+c and b+c = a+c (if a=b), so we have two numbers equal to a+b? Actually a+b=2a, and a+c and b+c both equal a+c (since b=a). So there could be duplicates among pairwise sums. But the max (a+b+c) is unique. However, it's possible that the max appears twice if there is some coincidence? For a,b,c positive, a+b+c > max(a+b, a+c, b+c). So it is strictly larger. Therefore, among the four numbers, the maximum is unique? Not necessarily if two numbers are equal to the same value which is also the max? That would require a+b+c equals some pairwise sum, impossible. But what about a+b+c equals some other number that is not a pairwise sum? Only four numbers, all are either pairwise or total. So the max is unique. Therefore we can identify a+b+c as the maximum of the four numbers.
Thus, let total = max(x1, x2, x3, x4). Then we have three other numbers which are the pairwise sums: say p, q, r (in some order). We need to find a, b, c such that:
a+b = one of them,
a+c = one,
b+c = one,
and a+b+c = total.
We can solve: given total, we can compute a, b, c from pairwise sums:
If we have a+b = S_ab, a+c = S_ac, b+c = S_bc, and total T = a+b+c.
Then we can compute:
a = (S_ab + S_ac - S_bc)/2? Actually from equations:
a+b = S_ab
a+c = S_ac
b+c = S_bc
T = a+b+c
We can also compute: a = T - (b+c) = T - S_bc
b = T - S_ac
c = T - S_ab
Check: Since T = a+b+c, then b+c = S_bc => a = T - S_bc, indeed. Similarly, b = T - S_ac, c = T - S_ab.
Thus, if we can correctly assign which of the three remaining numbers corresponds to which pairwise sum (ab, ac, bc), we can compute candidate a,b,c. However, we don't know which is which. But note that the three numbers are symmetric, and a,b,c can be output in any order. So we can assign the three remaining numbers arbitrarily to S_ab, S_ac, S_bc? Not exactly; we must ensure that the computed a,b,c are positive integers and that indeed the sums match. However, given that the problem guarantees a solution exists, if we try all permutations of the three remaining numbers (3! = 6) as (ab, ac, bc) and compute a,b,c via the formulas above, we will find at least one assignment that yields positive integers and also that the pairwise sums match? But we might also get positive integers even for wrong assignments? Possibly, but the problem says answer exists, so there is a correct assignment. However, is it possible that multiple assignments yield positive integers? Possibly, but we can just test each permutation and pick one that yields positive integers (and perhaps check that the sums actually equal the given numbers? Actually, if we compute a,b,c from T and assumed ab,ac,bc, then automatically the three sums will be as assigned? Not exactly: a = T - S_bc, b = T - S_ac, c = T - S_ab. Then we can compute a+b = (T - S_bc)+(T - S_ac) = 2T - (S_bc+S_ac). But we want it to equal S_ab. For consistency, we need to check that indeed a+b = S_ab, etc. However, the formulas are derived under the assumption that the assignment is correct, but if assignment is wrong, the computed a,b,c may not satisfy the sums. However, we can compute and then verify if a+b equals the number we assigned as S_ab, etc. But maybe it's simpler: Since the answer exists, the correct permutation will yield integer positive a,b,c and they'll be consistent. For wrong permutations, we might get negative or non-integer or perhaps positive but sums don't match? But we can just check a,b,c positive integers and also that if we recompute the four numbers (a+b, a+c, b+c, a+b+c) and sort and compare to sorted input? That would be safe. But we could also rely on the condition that a = T - S_bc, etc., which ensure that the sums are consistent? Actually, note: If we set S_ab, S_ac, S_bc arbitrarily, then we compute a = T - S_bc, b = T - S_ac, c = T - S_ab. Then:
a+b = (T - S_bc)+(T - S_ac) = 2T - (S_bc+S_ac). For this to equal S_ab, we need 2T = S_ab + S_ac + S_bc. But note that T = a+b+c, and the sum of all three pairwise sums = (a+b)+(a+c)+(b+c)=2(a+b+c)=2T. So indeed, for the correct assignment, we have S_ab + S_ac + S_bc = 2T. That is a necessary condition. If we assign arbitrarily, this equation might not hold. However, it's possible that for an incorrect permutation, the sum might also equal 2T by coincidence? Possibly, but then the computed a,b,c would be consistent? Let's test: Suppose we have numbers: say correct pairing yields sums S_ab, S_ac, S_bc. Their sum is 2T. If we permute labels, the three numbers are still the same set, so any permutation of the three numbers still sums to 2T. Because the three numbers themselves are the same values, just labeled differently. So S1+S2+S3 = 2T always, regardless of how we assign them to ab,ac,bc, because the sum of the three numbers is fixed. So the sum condition holds automatically. So if we take the three numbers, sum them, it equals 2T? Check: Let the three pairwise sums be p, q, r. Their sum = (a+b)+(a+c)+(b+c)=2(a+b+c)=2T. So yes. So for any permutation, p+q+r = 2T. Thus the condition that 2T = S_ab+S_ac+S_bc is always true if we use the three numbers (the non-max ones). So that condition does not differentiate.
Now compute a,b,c as above: a = T - assigned_bc, b = T - assigned_ac, c = T - assigned_ab. For the correct assignment, these will be positive integers (since pairwise sums < T). For an incorrect assignment, we might get some of these computed values negative or non-integer? But they are integers since T and the assigned numbers are integers. They could be negative if assigned number > T, but since we picked T as max, all others are < T, so T - assigned > 0, so a,b,c are positive for any assignment? Wait: If we assign a number to bc that is not actually b+c but perhaps a+b? Then T - (a+b) = c, which is positive. If we assign a+b to bc, then a = T - (a+b) = c, which is positive. Similarly, if we assign a+c to bc, a = T - (a+c) = b, positive. If we assign b+c to bc, a = T - (b+c) = a, positive. So for any assignment, each computed value is some permutation of a,b,c (or possibly other combinations). Indeed, compute:
Let the three numbers be P, Q, R. We assign them to ab, ac, bc. Then we compute:
a' = T - bc (the number assigned to bc)
b' = T - ac
c' = T - ab
Now if bc is actually the number representing (say) a+b, then a' = T - (a+b) = c.
If bc is a+c, then a' = T - (a+c) = b.
If bc is b+c, then a' = T - (b+c) = a.
Similarly, b' = T - ac: if ac is a+b => b' = T - (a+b) = c; if ac is a+c => b' = T - (a+c) = b; if ac is b+c => b' = T - (b+c) = a.
And c' = T - ab: if ab is a+b => c' = T - (a+b) = c; if ab is a+c => c' = T - (a+c) = b; if ab is b+c => c' = T - (b+c) = a.
So essentially, for any permutation, the triple (a', b', c') is a permutation of (a, b, c). Indeed, because each of a',b',c' will be one of a,b,c, possibly with duplicates if assignments cause two of them to be the same? But a,b,c could be equal; but even if not, the mapping might produce a valid triple but not necessarily in correct order. But the problem allows output in any order. So any permutation of the three numbers will yield a triple (a',b',c') that is some permutation of (a,b,c)?? Let's test: Suppose correct mapping: ab = A, ac = B, bc = C. Then a = T - C, b = T - B, c = T - A. Now if we permute assignments, what do we get? The set {T - p for p in {A,B,C}} is exactly {a,b,c} because T-A = c, T-B = b, T-C = a. So the set is {a,b,c} regardless of assignment, because the three numbers A,B,C are just being subtracted from T, and T-A, T-B, T-C are exactly c, b, a (some order). Actually {T-A, T-B, T-C} = {a,b,c}? Let's compute: correct numbers: A = a+b, B = a+c, C = b+c. Then T = a+b+c. So T - A = c, T - B = b, T - C = a. So set = {a,b,c}. If we assign differently, we are just permuting which of A,B,C we call ab, ac, bc. But the resulting triple (a',b',c') is (T - (assigned bc), T - (assigned ac), T - (assigned ab)). Since we are just selecting three numbers from A,B,C in some order to plug into those formulas, the multiset of the three computed numbers will always be {c,b,a} (or {a,b,c} anyway same set) because we are just computing T minus each of the three numbers in some order. So the three results are exactly the three values T-A, T-B, T-C, just possibly assigned to different variables a',b',c'. That means for any permutation of assignments, the triple (a',b',c') is a permutation of the correct (a,b,c). Thus all permutations yield positive integers (since a,b,c positive). So we cannot distinguish the correct assignment from an incorrect one by positivity. Indeed, any assignment will produce a valid triple of numbers that are a, b, c in some order. And those triple numbers will satisfy that if we compute a'+b', a'+c', b'+c', a'+b'+c' we get the original four numbers? Let's check: If we output (a',b',c') = (c, b, a) for instance, then a'+b' = c+b = b+c, which is one of the given numbers; a'+c' = c+a = a+c; b'+c' = b+a = a+b; and sum = a+b+c = T. So indeed, any permutation of (a,b,c) is a valid answer because we can output them in any order. The problem says: Print such positive integers a, b and c that four numbers written on a board are values a+b, a+c, b+c and a+b+c printed in some order. So as long as we output a multiset of three numbers that are exactly the correct a,b,c (maybe permuted), it's fine. So we don't need to worry about which assignment is correct. We just need to compute a, b, c up to permutation.
Thus a simple method: Identify the maximum as total = max(x1..x4). Then take the other three numbers (call them p, q, r). Then compute:
a = total - p,
b = total - q,
c = total - r.
But this yields three numbers. Are these guaranteed to be the desired a,b,c? Let's test with example: Input: 3 6 5 4. Sorted? max = 6? Actually numbers: 3,6,5,4. Max = 6. Others: 3,5,4. Compute a=6-3=3, b=6-5=1, c=6-4=2. That yields (3,1,2). In some order, that's 1,2,3. But expected output: 2 1 3 (which is 1,2,3). So it works.
Second example: 40 40 40 60. Max = 60. Others: three 40's. Compute a = 60-40=20, b=60-40=20, c=60-40=20. So (20,20,20). Good.
Third: 201 101 101 200. Max = 201? Wait numbers: 201,101,101,200. Max = 201. Others: 101,101,200. Compute a=201-101=100, b=201-101=100, c=201-200=1. So (100,100,1) which is correct.
But careful: In third example, pairwise sums: a+b? Let's compute a=100,b=100,c=1 gives sums: a+b=200, a+c=101, b+c=101, total=201. So indeed the numbers are 200,101,101,201. Input: 201,101,101,200. So matches.
Thus the algorithm is straightforward: find the max total, then for each of the other three numbers, compute total - value, and output those three results (in any order). However, we need to be careful: The maximum might not be unique if there are duplicate max values? But as argued, it's impossible for a+b+c to equal any pairwise sum, so the maximum value is strictly greater than all pairwise sums, thus it is unique. However, the input could contain duplicate numbers: e.g., a=b=c=2: pairwise sums = 4,4,4, total = 6. So numbers: 4,4,4,6. Here max = 6 unique. So it's unique. Could there be a case where two numbers equal the maximum? Only if there are two numbers both equal to a+b+c, but we have only one total sum, so that can't happen. So we can safely pick max.
But wait, there is a nuance: The numbers are positive integers, but what if a,b,c are such that some pairwise sum equals the total? That would require one of the numbers to be zero. Since they are positive, not possible. So max unique.
Thus algorithm:
Read four ints.
Find maximum value M.
For each of the four numbers, if it is not M, push it into a list (or subtract from M and output).
But we have four numbers; we need to take the other three. However, if there are duplicate numbers equal to M? Not possible, but still we can handle: we can collect all numbers, find max, then output (max - xi) for each xi != max. But that yields three numbers. However, if there is duplicate max? Not possible, but we can still handle: we could take all four, sort, then the max is the last, but if the last two are equal? That would imply two numbers equal to max, which is impossible given problem guarantee of existence. So okay.
Edge case: The numbers may be unsorted and have duplicates of non-max values. But our method yields correct triple.
But wait, need to confirm that a,b,c are positive. Since each other number < max (since max = total), total - xi > 0. So they are positive.
Now what about potential integer overflow? Numbers up to 1e9, subtractions fine in 64-bit.
Thus the code can be:
Read four ints into array x[4].
Find max_val = max(x).
Create result array.
For each xi: if xi != max_val, compute max_val - xi and add to result. But we must ensure we only take three numbers. Since there is exactly one max (guaranteed), we will get exactly three numbers.
Output the three numbers.
But careful: In case there are duplicates equal to max, but the problem says answer exists, so not. So fine.
Alternatively, we can compute: sort x, then output x[3]-x[0], x[3]-x[1], x[3]-x[2] (since sorted ascending). That yields same.
Thus solution complexity O(1).
Let's double-check reasoning: Why is max necessarily the total sum? Because a,b,c are positive, so each pairwise sum is strictly less than total sum. However, if a,b,c could be zero? But they are positive. Input numbers are at least 2, but that doesn't guarantee positivity? Problem statement: "Polycarp has guessed three positive integers a, b and c." So yes, positive.
Thus answer.
But wait, could there be a scenario where the maximum is not unique due to input being something like a+b = a+b+c? That's impossible. So maximum unique.
Thus solution is correct.
But the problem might have a twist: Some given numbers might be equal? Example: a=b=10, c=10? Actually a=b=c=10 -> sums: 20,20,20,30. So max 30 unique. So fine.
Thus implementation is trivial.
But before finalizing, let's consider if there could be any corner case where the max is not the total? Suppose a,b,c positive but very small? For instance a=1,b=1,c=1 -> max=3, pairwise sums=2,2,2. So yes.
Now, is it possible that the maximum appears more than once because of duplicates among the pairwise sums equaling the total? That would require a+b = a+b+c => c=0, impossible. So no.
Therefore the solution is as described.
Thus code: read four ints, find max, for each xi, if xi != max, print max - xi (space-separated). Since order doesn't matter, we can just output them.
Alternatively, we can sort and then compute differences.
Let's test with custom cases:
Case: a=5,b=5,c=7 -> sums: 10,12,12,17. Input: 10,12,12,17. Max=17, others: 10,12,12 => a=7, b=5, c=5? Actually 17-10=7, 17-12=5, 17-12=5 -> (7,5,5). That matches a,b,c (5,5,7) in some order. Good.
Case: a=100,b=200,c=300 -> sums:300,400,500,600. Input: 300,400,500,600. Max=600, others:300,400,500 => outputs:300,200,100. That's (100,200,300) in some order. Good.
Now what about a case where two numbers among the input are equal to each other and both could be considered as max? Eg: a=2,b=2,c=1 -> sums:4,3,3,5? Actually: a=2,b=2,c=1: a+b=4, a+c=3, b+c=3, total=5. So numbers: 4,3,3,5. Max=5 unique. Good.
Thus solution.
Implementation details: In C++, we can read ints, store in vector, find max via *max_element, then loop and if element != max, print max - element.
But note: There might be multiple occurrences of the max? Not possible, but if we use condition "if (x != max)" and there are duplicates of max, we would output fewer numbers. However, the guarantee ensures that the answer exists, which implies a,b,c positive and max is total, so there is exactly one max. So safe.
Better to sort: sort arr, then output arr[3]-arr[0], arr[3]-arr[1], arr[3]-arr[2]. This automatically handles duplicates: if arr[3] is duplicated, then arr[2] == arr[3], and we would output arr[3]-arr[2] = 0, which is not positive. So sorting method would fail if max is not unique. But since max is unique, sorting works. But to be safe, we can do the first method, but also guarantee exactly three numbers printed.
Edge: If there is a scenario where the maximum appears more than once due to some error, but problem says answer exists, so no.
Thus final answer: straightforward.
Now, let's think about proof: Given four numbers, the largest is the total sum. Then subtracting the other three from the total gives the three numbers a, b, c. Because total - (a+b) = c, etc.
Therefore, the solution.
Now, I'll produce final answer accordingly.
But the problem might be from Codeforces and many solutions do exactly that. I recall a known problem: "A. Polycarp and Sums of Subsets" or something. Yes, it's CF 1618A? Actually there is a problem "Polycarp and Sums of Subsets" but that's with 7 numbers. Another is "A. Sum of Three". This one is similar to CF Round? Actually it's "A. Polycarp and Sums of Subsets" maybe not. But it's a simple problem.
Thus final code.
Now in the answer, I need to output a solution in any programming language. Usually we present solution in Python for clarity.
Implementation:
def solve():
x = list(map(int, input().split()))
total = max(x)
res = []
for v in x:
if v != total:
res.append(total - v)
else:
# Mark that we have used total, to avoid multiple subtractions if duplicate total? but not.
# We'll set a flag to ignore after first occurrence.
total = None? Simpler: just collect all v != max and compute.
# But careful: If there are duplicates of max, we would compute for each v != max and maybe get fewer than 3 numbers. But we need exactly 3 numbers. But since guarantee only one max, this yields exactly 3 numbers.
# But to be safe, we can take max_val = max(x), then for each v in x: if v != max_val, compute; but that would compute for all three non-max. However if max appears twice, we'd only get two numbers. So we need to ensure we output three numbers. But given constraints, it's fine.
# Alternatively, we can use: sorted_x = sorted(x); a = sorted_x[3] - sorted_x[0]; b = sorted_x[3] - sorted_x[1]; c = sorted_x[3] - sorted_x[2]; print(a,b,c)
code
# I'll use the sorted method but with caution: only if sorted_x[3] is unique? Actually sorted_x[3] could equal sorted_x[2] if there is duplicate max, but then sorted_x[3]-sorted_x[2] = 0, which is invalid. But it's guaranteed that answer exists, so max is unique, thus sorted_x[2] < sorted_x[3]. So sorting method is safe under guarantee.
Let's implement with sorting:
x = sorted(list(map(int, input().split())))
a = x[3] - x[0]
b = x[3] - x[1]
c = x[3] - x[2]
print(a, b, c)
Check: Example 1: 3,6,5,4 sorted: [3,4,5,6] => a=6-3=3, b=6-4=2, c=6-5=1 => output 3 2 1 (or any order). That's valid.
Example 2: 40,40,40,60 sorted: [40,40,40,60] => a=20,b=20,c=20 -> correct.
Edge: If there is a scenario where the input numbers are such that the max is not the total? But guarantee says answer exists, so that ensures the numbers are indeed of the form described. So sorting method is fine.
Thus final answer.
Now produce answer as Python code.
But the problem statement has multiple examples. We'll output solution.
Now write final answer accordingly.
We should include explanation in the answer? The user asked: "You have to guess three numbers a, b and c using given numbers. Print three guessed integers in any order." They provided problem statement. Usually they want code. So I'll provide a clear solution.
Now let me produce final answer. ```python
def main():
# Read the four integers
x = list(map(int, input().split()))
# Sort them; the largest is a+b+c
x.sort()
total = x[3]
# Compute a, b, c (order does not matter)
a = total - x[0]
b = total - x[1]
c = total - x[2]
print(a, b, c)