Problem statement When you heard that this year's KUPC can be joined as a team, you…
- User
Problem statement
When you heard that this year's KUPC can be joined as a team, you decided to talk to your friends and join as a team.
Eventually, people, including you, got together.
Individual strength is expressed in ratings, and person ratings are , , , and , respectively.
You decided to split into teams of people each.
At this time, the strength of the team is defined by the sum of the ratings of the people who make up the team. In addition, the difference in ability between teams is defined by the absolute value of the difference in strength of each team.
You want to divide the teams so that the difference in ability between the teams is as small as possible.
Find the minimum value of the difference in ability between teams when the teams are divided successfully.
Constraint
- All inputs are integers
input
Input is given from standard input in the following format.
output
Output the minimum difference in ability between teams in an integer line.
Input example 1
2 1 3 4
Output example 1
0
If you team up with the and people, and the and people, the strength of the team will be and the difference in ability will be . Obviously this is the smallest.
Input example 2
64 224 239 1024
Output example 2
625
Example
Input
2 1 3 4
Output
0
Assistant
We are given four integers a,b,c,d representing ratings of four people. We need to split them into two teams of two each. The strength of a team is sum of the ratings of the two members. The difference between teams is absolute difference of strengths. We need to minimize this difference. Output the minimum possible absolute difference.
Constraints: ratings between 1 and 2799, integer. So all sums are up to 5598, difference up to 5597.
We need to compute all possible ways to partition four distinct people into two unordered pairs. Number of ways: There are 3 ways to pair up four items: (1) pair (a,b) and (c,d), (2) (a,c) and (b,d), (3) (a,d) and (b,c). For each, compute sum1 and sum2, absolute difference, and output minimum.
We can simply compute three differences and output the minimum.
Edge cases: If ratings are equal? Not necessary special.
Thus solution: read a,b,c,d; compute diff1 = abs((a+b)-(c+d)), diff2 = abs((a+c)-(b+d)), diff3 = abs((a+d)-(b+c)); answer = min(diff1, diff2, diff3). Print answer.
We need to confirm correctness: For four distinct individuals, these three partitions cover all distinct pairings because order of teams doesn't matter and teams are unordered.
Complexity O(1). Implementation straightforward.
Let's code in Python.
We might also consider sorting? But not needed.
We'll parse input, compute.
Potential pitfalls: Using int, absolute value. Input format: four integers separated by spaces.
We'll output integer.
That's it.
Thus final answer: min difference.
I'll produce a Python solution.
Testing with examples: Example1: a=2,b=1,c=3,d=4. diff1: (2+1)-(3+4)=3-7=-4 abs4; diff2: (2+3)-(1+4)=5-5=0; diff3: (2+4)-(1+3)=6-4=2; min=0. Output 0. Example2: 64 224 239 1024. Compute diff1: (64+224=288) - (239+1024=1263) = -975 abs975; diff2: (64+239=303) - (224+1024=1248) = -945 abs945; diff3: (64+1024=1088) - (224+239=463) = 625 abs625; min=625. Output 625.
Matches.
Thus solution is correct.
Now produce final answer. ```python
def main(): a, b, c, d = map(int, input().split()) diff1 = abs((a + b) - (c + d)) diff2 = abs((a + c) - (b + d)) diff3 = abs((a + d) - (b + c)) ans = min(diff1, diff2, diff3) print(ans)
if name == "main": main()