- 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
- 98 lines of Python from the credited upstream file ccc25s3.py.
- The implementation visibly relies on 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.
123 45mostpretty = 10**116leastpretty = 10**117total = 08 9n, m, q =map(int, input().split())10 11cpu = [[0,0,0]]*n12storage = [[0, 0, 0]]*(q+1)13 14prettypercolor = [0]*(m+1)15 1617for i in range(n):18 c, p = map(int, input().split())19 20 if p > prettypercolor[c]:21 prettypercolor[c] = p22 cpu[i] = [c, p, 1]23 if p < mostpretty:24 mostpretty = p25 for x in range(i):26 if cpu[x][0] == c and cpu[x][2] == 1:27 cpu[x][2] = 028 break29 else:30 cpu[i] = [c, p, 0]31 32def main():33 global mostpretty34 leastpretty = 10**1135 total = 036 37 for i in range(n):38 if cpu[i][2] == 1 and cpu[i][1] < leastpretty:39 leastpretty = cpu[i][1]40 cpu[i][2]=241 for x in range(i):42 if cpu[x][2] == 2:43 cpu[x][2] = 144 break45 46 47 for i in range(n):48 if cpu[i][2] == 0 and cpu[i][1] > mostpretty:49 mostpretty = cpu[i][1]50 cpu[i][2]=351 for x in range(n):52 if cpu[x][2] == 2:53 cpu[x][2] = 054 for x in range(i):55 if cpu[x][2] == 3:56 cpu[x][2] = 057 break58 59 60 for i in range(n):61 if cpu[i][2] == 1 or cpu[i][2] == 2 or cpu[i][2] == 3:62 total += cpu[i][1]63 print(total)64 65for i in range(q):66 a, b, num = map(int, input().split())67 storage[i] = [a, b, num]68 69main()70for i in range(q):71 mostpretty = 1072 73 if storage[i][0] == 1:74 cpu[storage[i][1]-1][0] = storage[i][2]75 else:76 cpu[storage[i][1]-1][1] = storage[i][2]77 78 for x in range(n):79 cpu[x][2] = 080 81 for x in range(m):82 prettypercolor[x+1] = 083 84 85 for x in range(n):86 if cpu[x][1] >= prettypercolor[cpu[x][0]]:87 prettypercolor[cpu[x][0]] = cpu[x][1]88 cpu[x][2] = 189 mostpretty = cpu[x][1]90 for y in range(x):91 if cpu[y][0] == cpu[x][0] and cpu[y][2] == 1:92 cpu[y][2] = 093 break94 else:95 cpu[x][2] = 096 97 main()98