A - 合格者への拍手 / Applause for the Successful Candidates 解説 by admin
Claude 4.5 OpusOverview
This is a problem where we need to select participants whose scores are \(K\) or higher, and find the sum of those participants’ scores.
Approach
The key points in this problem are the following two:
- Condition for being awarded: Only participants with a final score of \(K\) or higher are awarded
- Number of applauses: For each awarded participant, we applaud the same number of times as their score
In other words, we need to find all “participants with scores of \(K\) or higher” and sum up their scores.
Concrete Example
For example, consider the case where \(N = 5\), \(K = 60\), and the scores are \([50, 70, 80, 40, 90]\).
- Score \(50\): \(50 < 60\), so not awarded
- Score \(70\): \(70 \geq 60\), so awarded → \(70\) applauses
- Score \(80\): \(80 \geq 60\), so awarded → \(80\) applauses
- Score \(40\): \(40 < 60\), so not awarded
- Score \(90\): \(90 \geq 60\), so awarded → \(90\) applauses
Total: \(70 + 80 + 90 = 240\) applauses
Is a Naive Approach Sufficient?
In this problem, \(N \leq 10^5\), and we only need to check each participant once. No special techniques are required, and simply looping through all participants is fast enough to solve the problem.
Algorithm
- Read the number of participants \(N\) and the threshold \(K\)
- Read all participants’ scores \(P_1, P_2, \ldots, P_N\)
- For each score \(P_i\), determine whether \(P_i \geq K\)
- Calculate the sum of scores that satisfy the condition
- Output the result
In Python, this can be written concisely using a conditional list comprehension:
total = sum(p for p in P if p >= K)
This means “sum all elements \(p\) in \(P\) that are \(K\) or higher.”
Complexity
- Time complexity: \(O(N)\)
- Because we check each participant’s score once
- Space complexity: \(O(N)\)
- Because we store all participants’ scores in a list
Implementation Notes
By passing a generator expression to Python’s
sum()function, we can efficiently calculate the sum of elements that satisfy the condition with good memory usageUsing the
ifclause in list comprehension allows us to write filtering and aggregation concisely in one lineEven when there are 0 awarded participants,
sum()correctly returns \(0\), so no special case handling is neededSource Code
N, K = map(int, input().split())
P = list(map(int, input().split()))
total = sum(p for p in P if p >= K)
print(total)
This editorial was generated by claude4.5opus.
投稿日時:
最終更新: