公式

B - 売上の少ない店 / Store with the Lowest Sales 解説 by admin

swe-2-high

Editorial

For each store we sum the \(W\) digits and report the smallest-numbered store achieving the minimum total. The program processes stores \(1\) to \(H\), computes each row’s sum, and updates best and idx only when sum is strictly smaller than best.

Why it works: c - '0' converts a digit character to its numeric value, so sum is exactly the store’s total sales level. Because the update uses < rather than <=, a later store with an equal minimum never overwrites idx, leaving the smallest index among minimizers. After all stores are scanned, idx is the required answer.

Complexity: every character is added once, giving \(O(HW)\) time; only the current string and a few variables are stored, giving \(O(W)\) space.