内容中心

2026年质量好的塑料设备焊接工程长期合作厂家推荐-泸州欧亚达防腐设备有限公司

To solve this problem, we need to modify a list of integers such that any element smaller than a given threshold is replaced by the threshold. Then, we compute the sum of the modified list.

Approach

  1. Input Reading: First, we read the number of elements n and the threshold k from the input.
  2. List Modification: For each element in the list, if the element is smaller than k, replace it with k; otherwise, keep the element as it is. This can be efficiently done using the max function (since max(x, k) gives k if x < k and x otherwise).
  3. Sum Calculation: Compute the sum of the modified list and print the result.

Solution Code

n, k = map(int, input().split())
arr = list(map(int, input().split()))
print(sum(max(x, k) for x in arr))

Explanation

  • Reading Input: The first line of input is split into n (number of elements) and k (threshold). The second line is split into a list of integers.
  • Modifying Elements: Using a generator expression, we iterate over each element in the list and apply max(x, k) to get the modified value.
  • Summing: The sum of these modified values is computed directly using the sum function, which is efficient and concise.

This approach ensures that we handle the list in linear time O(n) (since each element is processed once), making it optimal for the given problem constraints. The solution is both simple and efficient.

Sample Input: 5 3
1 2 3 4 5

Sample Output: 18

Explanation: The modified list is [3,3,3,4,5], sum is 3+3+3+4+5 =18.

Another Test Case: Input: 3 5
2 6 4

Output:16

Explanation: Modified list is [5,6,5], sum is5+6+5=16.

This solution works for all valid inputs and meets the problem requirements.

泸州欧亚达防腐设备有限公司



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

在线客服

在线留言
您好,很高兴为您服务,可以留下您的电话或微信吗?