- 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
- 74 lines of Python from the credited upstream file arc096_b.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, c = map(int, input().split())10 x = [0 for _ in range(n)]11 v = [0 for _ in range(n)]12 13 for i in range(n):14 xi, vi = map(int, input().split())15 x[i] = xi16 v[i] = vi17 18 def calc():19 from itertools import accumulate20 21 22 a = list(accumulate(v))23 24 b = list(accumulate(v[::-1]))[::-1]25 26 27 y = [c - xi for xi in x]28 29 30 d = [0 for _ in range(n)]31 d[n - 1] = b[n - 1] - y[n - 1]32 33 for i in range(n - 2, -1, -1):34 d[i] = b[i] - y[i]35 d[i] = max(d[i], d[i + 1])36 37 candidate = 038 39 40 for i, (ai, xi) in enumerate(zip(a, x)):41 sum_cal = 042 sum_cal -= xi 43 sum_cal += ai 44 45 candidate = max(candidate, sum_cal)46 47 48 sum_cal -= xi 49 50 if i < n - 1:51 sum_cal += d[i + 1]52 53 candidate = max(candidate, sum_cal)54 55 return candidate56 57 ans = 058 59 60 result = calc()61 ans = max(ans, result)62 63 64 v = v[::-1]65 x = [c - xi for xi in x][::-1]66 result = calc()67 ans = max(ans, result)68 69 print(ans)70 71 72if __name__ == "__main__":73 main()74