- 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 abc308_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 mex(x, y, z):5 used = [False] * 46 7 for i in [x, y, z]:8 used[i] = True9 10 for i, u in enumerate(used):11 if not u:12 return i13 14 15def main():16 import sys17 18 input = sys.stdin.readline19 20 n = int(input())21 a = list(map(int, input().split()))22 s = input().rstrip()23 24 25 acc_m = [[0 for _ in range(n)] for _ in range(3)]26 acc_x = [[0 for _ in range(n)] for _ in range(3)]27 28 for i, (si, ai) in enumerate(zip(s, a)):29 if i > 0:30 for y in range(3):31 acc_m[y][i] = acc_m[y][i - 1]32 acc_x[y][i] = acc_x[y][i - 1]33 34 if si == "M":35 acc_m[ai][i] += 136 elif si == "X":37 acc_x[ai][i] += 138 39 ans = 040 41 for j, (si, ai) in enumerate(zip(s, a)):42 if j == 0 or j == n - 1:43 continue44 45 if si != "E":46 continue47 48 for x in range(3):49 for y in range(3):50 ans += acc_m[x][j - 1] * (acc_x[y][n - 1] - acc_x[y][j]) * mex(x, y, ai)51 52 print(ans)53 54 55if __name__ == "__main__":56 main()57