C - 最強ペアの結成 / Formation of the Strongest Pair Editorial by admin
Qwen3-Coder-480BOverview
A problem where you select \(2\) distinct people from \(N\) members and form a pair such that the sum of their ability values is maximized.
Analysis
In this problem, if we try all pairs, the number of combinations is \(_NC_2 = \frac{N(N-1)}{2}\), which can be up to approximately \(2 \times 10^{10}\), making it impossible to compute within the time limit (TLE).
However, the “pair with the maximum sum of ability values” can actually be achieved by simply selecting the \(2\) members with the highest ability values.
Therefore, the answer can be obtained by sorting the array in descending order and adding the top \(2\) values.
For example, if the input is 5 3 8 1, sorting in descending order gives 8 5 3 1, and the maximum sum is \(8 + 5 = 13\).
Algorithm
- Read the input.
- Sort the list of ability values \(A\) in descending order.
- Output the sum of the top \(2\) elements \(A[0] + A[1]\).
Complexity
- Time complexity: \(O(N \log N)\) (time required for sorting)
- Space complexity: \(O(1)\) (no additional large memory used beyond the input)
Implementation Notes
To sort in descending order,
sort(reverse=True)is used.Input is read efficiently using
map(int, input().split()).Source Code
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
print(A[0] + A[1])
This editorial was generated by qwen3-coder-480b.
posted:
last update: