Official

B - 果物の選別 / Fruit Sorting Editorial by admin

Claude 4.5 Opus

Overview

This problem asks us to count only the fruits with defects that have sweetness below the threshold \(K\) as “substandard products,” and find both the count and the sum of their sweetness values.

Analysis

Problem Breakdown

What we need to find in this problem are fruits that satisfy both of the following conditions: 1. Has a defect (included in list \(B\)) 2. Sweetness is less than \(K\) (does not meet the premium product criteria)

In other words, the key rule is: “Even if a fruit has a defect, it doesn’t become substandard if its sweetness is \(K\) or higher.”

Understanding Through an Example

For example, with \(N=5, M=3, K=50\), sweetness values \(A = [30, 60, 45, 80, 25]\), and defective fruits \(B = [1, 2, 4]\):

Fruit Number Sweetness Has Defect? Sweetness ≥ K? Substandard?
1 30 × (30 < 50)
2 60 ○ (60 ≥ 50) ×
4 80 ○ (80 ≥ 50) ×

Result: 1 substandard product, total sweetness is 30

Approach

Since there are at most \(M\) defective fruits (\(M \leq N \leq 2 \times 10^5\)), checking all \(M\) fruits is fast enough. No special algorithm is needed—a simple loop will solve this.

Algorithm

  1. Receive the sweetness list \(A\) and the list of defective fruit numbers \(B\) as input
  2. Initialize the substandard count count and sweetness total total to 0
  3. For each defective fruit number \(b\):
    • Get the sweetness \(A_{b}\) of fruit \(b\) (since arrays are 0-indexed, use \(A[b-1]\))
    • If the sweetness is less than \(K\), increment count by 1 and add the sweetness to total
  4. Output the final count and total

Complexity

  • Time Complexity: \(O(N + M)\)
    • \(O(N)\) for reading the sweetness list \(A\)
    • \(O(M)\) for reading and iterating through the defective fruits list \(B\)
    • \(O(1)\) for accessing each fruit’s sweetness
  • Space Complexity: \(O(N + M)\)
    • \(O(N)\) for storing the sweetness list \(A\)
    • \(O(M)\) for storing the defective fruits list \(B\)

Implementation Notes

  • Index Conversion: In the problem statement, fruit numbers start from 1 (1-indexed), but Python lists start from 0 (0-indexed). Therefore, to get the sweetness of fruit number \(b\), you need to use A[b - 1]
  • Data Types: Since sweetness can be up to \(10^9\) and there can be up to \(2 \times 10^5\) fruits, the total can reach approximately \(2 \times 10^{14}\). In Python, there’s no need to worry about integer overflow, but in other languages, you need to use 64-bit integer types

Source Code

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

count = 0
total = 0

for b in B:
    sweetness = A[b - 1]  # Fruit numbers are 1-indexed
    if sweetness < K:  # Only substandard if sweetness is less than K
        count += 1
        total += sweetness

print(count, total)

This editorial was generated by claude4.5opus.

posted:
last update: