- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 73 lines of Python from the credited upstream file abc159_e.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12 3 4def can_add(j, k, t, group, group_id):5 for index in range(group_id):6 group[index] += t[index][j]7 8 if group[index] > k:9 return False, group10 11 return True, group12 13 14def main():15 import sys16 17 input = sys.stdin.readline18 19 h, w, k = map(int, input().split())20 s = [list(input().rstrip()) for _ in range(h)]21 t = [[0 for __ in range(w)] for _ in range(h)]22 ans = float("inf")23 24 for bit in range(1 << (h - 1)):25 id = [0 for _ in range(h)]26 current_id = 027 28 for j in range(h):29 id[j] = current_id30 31 if bit & (1 << j):32 current_id += 133 34 current_id += 135 36 37 for wi in range(w):38 for hi in range(h):39 t[hi][wi] = 040 41 42 for wi in range(w):43 for hi in range(h):44 t[id[hi]][wi] += int(s[hi][wi])45 46 ok = True47 48 for wi in range(w):49 for hi in range(h):50 if t[id[hi]][wi] > k:51 ok = False52 53 if not ok:54 continue55 56 candidate = current_id - 157 group = [0 for _ in range(current_id)]58 59 for wj in range(w):60 flag, group = can_add(wj, k, t, group, current_id)61 62 if not flag:63 candidate += 164 group = [t[i][wj] for i in range(current_id)]65 66 ans = min(ans, candidate)67 68 print(ans)69 70 71if __name__ == "__main__":72 main()73