Problem solution · Python

ABC216 E — Amusement Park

ABC216 E — Amusement Park: a Python solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to KATO-Hiro AtCoder Solutions.

Technique
Direct simulation
Source
KATO-Hiro AtCoder Solutions
Length
48 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For ABC216 E — Amusement Park, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 48 lines of Python from the credited upstream file abc216_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.

Source

Code and credit

This code comes from KATO-Hiro AtCoder Solutions by KATO-Hiro and is used under the CC0-1.0 licence.

Full codeABC216 E — Amusement Park · PythonPython
Use this to learn the idea, then write your own version.
# -*- coding: utf-8 -*-  def f(k, a, x):    count = 0     for ai in a:        count += max(ai - x, 0)        return count <= k  def main():    import sys     input = sys.stdin.readline     n, k = map(int, input().split())    a = list(map(int, input().split()))    ng = -1    ok = 2 * 10 ** 9 + 1     while ok - ng > 1:        mid = (ok + ng) // 2         if f(k, a, mid):            ok = mid        else:            ng = mid        summed = 0    ans = 0     for ai in a:        count = max(0, ai - ok)         if count > 0:            ans += (ai * (ai + 1) // 2) - (ok * (ok + 1) // 2)            summed += count        ans += (k - summed) * ok     print(ans)  if __name__ == "__main__":    main() 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗