公式

A - 合格者への拍手 / Applause for the Successful Candidates 解説 by admin

Claude 4.5 Opus

Overview

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:

  1. Condition for being awarded: Only participants with a final score of \(K\) or higher are awarded
  2. 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

  1. Read the number of participants \(N\) and the threshold \(K\)
  2. Read all participants’ scores \(P_1, P_2, \ldots, P_N\)
  3. For each score \(P_i\), determine whether \(P_i \geq K\)
  4. Calculate the sum of scores that satisfy the condition
  5. 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 usage

  • Using the if clause in list comprehension allows us to write filtering and aggregation concisely in one line

  • Even when there are 0 awarded participants, sum() correctly returns \(0\), so no special case handling is needed

    Source 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.

投稿日時:
最終更新: