- 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
- 57 lines of Python from the credited upstream file abc286_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 main():5 import sys6 7 input = sys.stdin.readline8 9 n = int(input())10 a = list(map(int, input().split()))11 s = [list(input().rstrip()) for _ in range(n)]12 inf = 10 ** 1513 p = [[(inf, 0) for _ in range(n)] for _ in range(n)]14 15 for i in range(n):16 for j in range(n):17 if s[i][j] == "Y":18 p[i][j] = (1, -a[j])19 20 def warshall_floyd(dist):21 '''22 Args:23 Distance matrix between two points.24 Returns:25 Matrix of shortest distance.26 Landau notation: O(n ** 3).27 '''28 29 v_count = len(dist[0])30 31 for k in range(v_count):32 for i in range(v_count):33 for j in range(v_count):34 (dist_ik, value_ik), (dist_kj, value_kj) = dist[i][k], dist[k][j]35 dist[i][j] = min(dist[i][j], (dist_ik + dist_kj, value_ik + value_kj))36 37 return dist38 39 results = warshall_floyd(p)40 q = int(input())41 42 for _ in range(q):43 ui, vi = map(int, input().split())44 ui -= 145 vi -= 146 47 dist, value_max = results[ui][vi]48 49 if dist == inf:50 print("Impossible")51 else:52 print(dist, a[ui] - value_max)53 54 55if __name__ == "__main__":56 main()57