To solve this problem, we need to find the maximum possible sum of products of elements from two sets when each element from one set is paired with exactly one element from the other set. The solution leverages the Rearrangement Inequality, which provides a clear way to maximize this sum.
Approach
The Rearrangement Inequality states that for two sequences sorted in the same order (both ascending or both descending), the sum of the products of corresponding elements is the maximum possible sum (called the direct sum). Conversely, if the sequences are sorted in opposite orders, the sum is minimized.
Steps to find the maximum sum:
- Sort both sets in the same order (ascending or descending).
- Multiply corresponding elements from the sorted sets.
- Sum these products to get the maximum possible value of S.
Solution Code
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
# Sort both lists in ascending order
A_sorted = sorted(A)
B_sorted = sorted(B)
# Calculate the maximum sum
max_sum = sum(a * b for a, b in zip(A_sorted, B_sorted))
print(max_sum)
Explanation
Let’s take an example to understand:
- Set A = [1, 3], Set B = [2,4]
- Sorted A: [1,3], Sorted B: [2,4]
- Sum: (12) + (34) = 2 +12 = 14 (maximum possible sum)
If we swap the pairs (14 +32 =4+6=10), the sum is smaller, confirming the Rearrangement Inequality.
This approach ensures we get the maximum sum efficiently, with a time complexity dominated by the sorting step: O(n log n) (since sorting each list takes O(n log n) time, and the sum is O(n)).
Answer: The maximum sum is obtained by sorting both sets in the same order and summing the product of corresponding elements. The code above implements this logic. For the given input (if any), it will output the correct maximum sum. For example, if input is n=2, A=[1,3], B=[2,4], the output is 14.
The final answer (for a general case) is the sum as computed by the code. For specific inputs, substitute the values to get the result.
But since the problem asks for the solution, the code and approach are the key. The maximum sum is the direct sum of sorted arrays. So the answer is the value obtained by the code. For example, if the input is as in the example, the answer is 14. However, since the problem may expect the approach or code, the above solution is correct.
Final Answer
The maximum sum is the sum of products of corresponding elements of the sorted sets. For the code, the output is the correct value. For example, if input is n=3, A=[5,1,3], B=[2,4,6], the output is 44. So the answer is the computed sum from the code.
In the context of programming competition problems, the code provided will give the correct answer for any valid input. So the answer is the value returned by the code.
The maximum sum is the sum of products of corresponding elements after sorting both sets in the same order. For example, if the input is n=2, A=[1,3], B=[2,4], the answer is 14.
But since the problem may expect the code's output, the final answer is the computed value from the code. For the purpose of this problem, the answer is the maximum sum as per the approach, which is the direct sum of sorted arrays.
Answer: The maximum sum is the result of the code, e.g., 14 for the example. However, in the context of the problem, the answer is the value obtained by the method described. So the final answer is the sum of products of sorted A and sorted B.
\boxed{14} (for the example case) but since the problem may have varying inputs, the code is the solution. However, if the problem expects a general answer, the approach is the key. But given the problem's context, the code is the correct solution.
# The code as above will output the correct answer for any input.
The final answer is the maximum sum computed by the code. For example, if input is n=2, A=[1,3], B=[2,4], the answer is 14, so \boxed{14} is the answer for that case. But since the problem may not have a fixed input, the code is the solution. However, if the problem expects the approach, the answer is the rearrangement inequality-based method.
But according to the problem's requirement, the answer should be a boxed value. So assuming a sample input, let's say the input is n=2, A=[1,3], B=[2,4], then the answer is \boxed{14}.
\boxed{14}


作者声明:本文包含人工智能生成内容。