Official

B - 売上の少ない店 / Store with the Lowest Sales Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

For each store, calculate the sum of the digits contained in the string. Choose the store with the smallest sum; if there are multiple stores with the same sum, output the one with the smallest index.

Analysis

The sum of sales levels for store \(i\) can be calculated by converting each character of the string \(S_i\) into an integer and summing them up. For example, if \(S_i =\) 203, the sum is \(2 + 0 + 3 = 5\).

A naive approach of examining every day for all stores is sufficient. Based on the constraints, there are at most \(H \times W = 100\) digits to check, which easily runs within the time limit.

One detail to keep in mind is that if the sums are equal, we must choose the store with the smaller index. We can satisfy this condition by checking the stores in ascending order of their indices and updating the answer only when a strictly smaller sum is found.

For example, if the sums for each store are \(5, 3, 3\) in order, the answer is updated at store \(2\) but not at store \(3\). As a result, store \(2\) is correctly selected. If we were to update on equal sums as well, we would select a store with a larger index, resulting in a wrong answer.

Algorithm

Maintain the following information:

  • best_sum: the minimum sum among the stores checked so far
  • best_idx: the smallest index among the stores that achieve this minimum sum

The procedure is as follows:

  1. Initialize best_sum to None and best_idx to -1.
  2. Process each store \(i = 1, 2, \ldots, H\) in order:
    • Read the string \(S_i\).
    • Convert each character to an integer and find the sum s.
    • If it is the first store or s < best_sum, update best_sum to s and best_idx to \(i\).
  3. Output best_idx.

After processing each store, best_idx stores “the store with the minimum sum and the smallest index among those checked so far.” Therefore, best_idx after processing all stores is the answer.

Complexity

  • Time complexity: \(O(HW)\)
    For each store, we examine all characters of a string of length \(W\).
  • Space complexity: \(O(W)\)
    We store only the string for \(1\) store at a time. The memory used for other variables is \(O(1)\).

Implementation Notes

  • You can compute the sum of the digits using sum(int(c) for c in S). Note that int(S) converts the entire string into a single integer, which is not what we want.

  • The update condition should be s < best_sum. Using s <= best_sum would overwrite with later stores in case of a tie.

  • By checking best_sum is None first, the first store is guaranteed to trigger an update, and it avoids comparing an integer with None.

  • Since store indices are 1-based, iterate through them using range(1, H + 1).

    Source Code

def main():
    import sys
    input = sys.stdin.readline
    H, W = map(int, input().split())
    best_idx = -1
    best_sum = None
    for i in range(1, H + 1):
        S = input().strip()
        s = sum(int(c) for c in S)
        if best_sum is None or s < best_sum:
            best_sum = s
            best_idx = i
    print(best_idx)

main()

This editorial was generated by gpt-6-astra-medium.

posted:
last update: