This page is still under construction.

Parts of this page are still being built. What you see may change.

Sliding Window Minimum

Interview

Time limit2.4sMemory limit512 MB

Summary
Print the minimum of each window of length L ending at every position of the array.
Level

Medium4 of 10

Topics
Sliding window, Queue
Solved
No attempts yet

Problem

You are given NN numbers A1,A2,…,ANA_1, A_2, \dots, A_N and a number LL.

Let DiD_i be the minimum of Ai−L+1A_{i-L+1} through AiA_i. That is, DiD_i is the smallest value in the window of length LL that ends at position ii. No AA with an index of 00 or less exists, so ignore those indices when computing DiD_i. When i<Li < L the window is shorter, and DiD_i is the minimum of A1A_1 through AiA_i.

Write a program that prints D1D_1 through DND_N.

Input

The first line contains NN and LL. (1≤L≤N≤5 000 0001 \le L \le N \le 5\,000\,000)

The second line contains the NN numbers AiA_i, separated by spaces. (−109≤Ai≤109-10^9 \le A_i \le 10^9)

Output

On the first line, print D1D_1 through DND_N in order, separated by spaces.

Examples4

  1. Example 1

    Input
    12 3
    1 5 2 3 6 2 3 7 3 5 2 6
    
    Expected output
    1 1 1 2 2 2 2 2 3 3 2 2
  2. Example 2

    Input
    1 1
    5
    
    Expected output
    5
  3. Example 3

    Input
    5 5
    3 1 4 1 5
    
    Expected output
    3 1 1 1 1
  4. Example 4

    Input
    5 1
    -1 2 -3 4 -5
    
    Expected output
    -1 2 -3 4 -5