Editorial for Sensor Blocking


Author:kahootist

Editorial

We can iterate through the array from left to right. When we see a sensor which needs blocking, we place down however many pieces of foil we need to cover it. We need to remember that this foil will already partially take care of the next kk sensors, so we add the amount of foil we add to a variable cc. We need to remember to subtract it again after kk iterations, so we add it to an array that keeps track of what to subtract. At every sensor with strength ss, the amount of foil we need to add will obviously be s−cs - c. Every time we add foil we add it to the total, which is what we return.

Implementation

Code 1
n, k = map(int, input().split())
a = list(map(int, input().split()))

cur = 0
end = [0] * n
total = 0

for i in range(n):
    cur -= end[i]
    if a[i] <= cur:
        continue
    q = a[i] - cur
    cur += q
    total += q
    if i + k < n:
        end[i + k] = q

print(total)

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.