A - 台風の被害調査 / Typhoon Damage Survey Editorial by admin
Qwen3-Coder-480BOverview
Given the height and restoration cost of each fruit tree, determine the total cost to replant all fruit trees that have fallen, where trees with height \(T\) or less have fallen.
Analysis
In this problem, we need to check for each fruit tree whether “its height is \(T\) or less,” and sum up the restoration costs of those that satisfy the condition (i.e., the fallen fruit trees).
A straightforward approach is to check all fruit trees in order and add the cost whenever one meets the condition. No special techniques are needed, and looking at the constraints, \(N\) is at most \(2 \times 10^5\), so performing constant-time operations on each element is fast enough.
A potential pitfall in this problem is mistakes in reading input or loop range errors, but it can be easily solved by properly processing the lists.
Algorithm
- Read the number of fruit trees \(N\) and the height threshold \(T\) for falling.
- Read the height \(H_i\) and cost \(C_i\) of each fruit tree as lists.
- For each fruit tree \(i\), if \(H_i \leq T\), add \(C_i\) to the total cost.
- Output the final total cost.
For example, suppose the input is as follows:
N=4, T=5
H = [3, 7, 5, 6]
C = [10, 20, 30, 40]
In this case, \(H_1=3 \leq 5\) and \(H_3=5 \leq 5\), so fruit trees 1 and 3 have fallen.
The corresponding costs are \(C_1=10\) and \(C_3=30\), so the total cost is \(10 + 30 = 40\).
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
Since we check each element exactly once, the processing runs in linear time. Storing the input requires \(O(N)\) memory.
Implementation Notes
Make sure the input is being read correctly (especially list indices).
Be careful not to mix up the comparison direction in the conditional
if H[i] <= T:.Don’t forget to initialize the summation variable
total_cost.Source Code
# 入力の読み込み
N, T = map(int, input().split())
H = list(map(int, input().split()))
C = list(map(int, input().split()))
# 総コストの計算
total_cost = 0
for i in range(N):
if H[i] <= T:
total_cost += C[i]
# 結果の出力
print(total_cost)
This editorial was generated by qwen3-coder-480b.
posted:
last update: