problems / 180

Suki's Treat Window Maximum

medium sliding-window monotonic-queue arrays 1000 ms · 256 MB

Suki's Treat Window Maximum

Suki loves her daily walks through the park, and today she's discovered a delightful row of treat piles! As she walks past them, her vision spans exactly k consecutive piles at any moment. She wants to know the maximum pile size she can see at each position during her walk.

More formally, given an array of n treat pile sizes, compute the maximum value in every contiguous subarray of length k, for all starting positions where a full window fits.

Input

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 2·10^5).

The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9), the sizes of the treat piles.

Output

Print n - k + 1 integers: the maximum pile size in Suki's window at each position. Print one integer per line.

Notes

The first window starts at position 0 and includes piles 0 through k-1. The second window starts at position 1 and includes piles 1 through k. And so on.

Example 1

Input

5 3
1 3 1 2 1

Output

3
3
2

Example 2

Input

3 1
5 2 8

Output

5
2
8

Hints · reveal one at a time if you're stuck

Hint 1

When the window slides one step, one value enters on the right and one leaves on the left. Can you avoid recomputing the maximum of each window from scratch?

Hint 2

Keep a queue of candidate indices whose values are decreasing. When a new value arrives, which earlier candidates can never be the maximum of a future window?

Hint 3

Remove candidates from the back that are not larger than the new value, and drop the front index once it leaves the window. The front of the queue is then the maximum of each window, and the total work is linear.

report an issue Prefer your own editor? sooks prep 180 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.